Loading...
Loading...
Implement Syncfusion Blazor DataForm component for creating dynamic, data-bound forms with validation and field management. Use this when building forms in Blazor with Syncfusion components, handling form validation, binding models, creating editable fields, or managing form events. This skill covers form layout customization, data binding, FormItems configuration, FormAutoGenerateItems setup, templates, events, and data annotation validation.
npx skill4agent add syncfusion/blazor-ui-components-skills syncfusion-blazor-dataform@page "/dataform-example"
@using System.ComponentModel.DataAnnotations
@using Syncfusion.Blazor.DataForm
<SfDataForm ID="MyDataForm"
Model="@employeeModel">
<FormValidator>
<DataAnnotationsValidator></DataAnnotationsValidator>
</FormValidator>
<FormItems>
<FormAutoGenerateItems></FormAutoGenerateItems>
</FormItems>
</SfDataForm>
@code {
public class EmployeeModel
{
[Required(ErrorMessage = "First Name is required")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Invalid email address")]
[Display(Name = "Email")]
public string Email { get; set; }
[Range(18, 65, ErrorMessage = "Age must be between 18 and 65")]
[Display(Name = "Age")]
public int Age { get; set; }
[Display(Name = "Date of Birth")]
public DateTime? DateOfBirth { get; set; }
}
private EmployeeModel employeeModel = new EmployeeModel();
}<SfDataForm ID="ContactForm" Model="@contact" OnValidSubmit="HandleValidSubmit">
<FormValidator>
<DataAnnotationsValidator></DataAnnotationsValidator>
</FormValidator>
<FormItems>
<FormItem Field="@nameof(contact.Name)" LabelText="Full Name"></FormItem>
<FormItem Field="@nameof(contact.Email)" LabelText="Email Address"></FormItem>
<FormItem Field="@nameof(contact.Message)" EditorType="FormEditorType.TextArea"></FormItem>
</FormItems>
</SfDataForm>
@code {
private Contact contact = new();
private async Task HandleValidSubmit(EditContext context)
{
// Save contact to database
await SaveContactAsync(contact);
}
}<SfDataForm Model="@product">
<FormItems>
<FormItem Field="@nameof(product.Name)" LabelText="Product Name"></FormItem>
<FormAutoGenerateItems></FormAutoGenerateItems>
<FormItem Field="@nameof(product.Category)" EditorType="FormEditorType.DropDownList"></FormItem>
</FormItems>
</SfDataForm>
@code {
private Product product = new();
}<SfDataForm Model="@order" OnUpdate="HandleFieldUpdate">
<FormItems>
<FormItem Field="@nameof(order.Country)" LabelText="Country"></FormItem>
<FormItem Field="@nameof(order.City)" LabelText="City"></FormItem>
<FormAutoGenerateItems></FormAutoGenerateItems>
</FormItems>
</SfDataForm>
@code {
private Order order = new();
private async Task HandleFieldUpdate(FormUpdateEventArgs args)
{
if (args.FieldName == nameof(Order.Country))
{
// Update cities based on selected country
order.City = await GetCitiesForCountryAsync(order.Country);
}
}
}<SfDataForm Model="@employee" ColumnCount="2">
<FormItems>
<FormItem Field="@nameof(employee.FirstName)" LabelText="First Name" ColumnSpan="1"></FormItem>
<FormItem Field="@nameof(employee.LastName)" LabelText="Last Name" ColumnSpan="1"></FormItem>
<FormItem Field="@nameof(employee.Email)" LabelText="Email" ColumnSpan="2"></FormItem>
<FormItem Field="@nameof(employee.Department)" LabelText="Department" ColumnSpan="1"></FormItem>
<FormItem Field="@nameof(employee.Salary)" LabelText="Salary" ColumnSpan="1"></FormItem>
</FormItems>
</SfDataForm>
@code {
private Employee employee = new();
}| Property | Type | Purpose |
|---|---|---|
| object | The data model bound to the form |
| EditContext | Blazor EditContext for advanced binding scenarios |
| int | Number of columns for form layout |
| EventCallback | Fires when form is submitted with valid data |
| EventCallback | Fires when form is submitted with invalid data |
| EventCallback | Fires on every submit attempt |
| EventCallback | Fires when a field value changes |
EditorType| FormEditorType | Use Case | When to Use |
|---|---|---|
| Single-line text input | Override for string fields (default), not needed for numeric types |
| Multi-line text input | When you need multi-line text entry for string properties |
| Date selection only | Override for DateTime (default for DateTime) |
| Date and time selection | When you need both date and time for DateTime properties |
| Time selection only | When you need only time selection for TimeSpan or DateTime |
| Dropdown selection from list | For enum or custom list selection |
| Editable dropdown with filtering | When you need searchable dropdown |
| Auto-complete with suggestions | When you need auto-suggest functionality |
| Boolean toggle (note: lowercase 'b') | Override for bool (default), NOT |
| Toggle switch for boolean | Alternative to checkbox for bool properties |
| Password input with masking | For password string fields |
intlongdecimaldoublefloatstringboolDateTimeenumCheckboxCheckBoxNumericTextBoxEditorType| Method | Purpose |
|---|---|
| Refresh form and re-render fields |
| Programmatically submit the form |
| Clear form and reset to initial values |
| Check if form is valid |
| Trigger validation without submitting |
EditorTypeEditorTypeEditorTypeFormEditorType.TextAreaFormEditorType.SwitchFormEditorType.DateTimePickerDatePickerFormEditorType.PasswordFormEditorType.DropDownListComboBoxFormEditorType.CheckboxFormEditorType.CheckBoxFormEditorType.NumericTextBox<!-- Numeric field - EditorType not needed, automatically renders numeric textbox -->
<FormItem Field="@nameof(model.Quantity)"
LabelText="Quantity">
</FormItem>
<!-- Boolean field - EditorType not needed, automatically renders checkbox -->
<FormItem Field="@nameof(model.IsActive)"
LabelText="Is Active">
</FormItem>
<!-- String field with TextArea override -->
<FormItem Field="@nameof(model.Description)"
LabelText="Description"
EditorType="FormEditorType.TextArea">
</FormItem>
<!-- Boolean field with Switch override -->
<FormItem Field="@nameof(model.IsEnabled)"
LabelText="Enabled"
EditorType="FormEditorType.Switch">
</FormItem>