# Blazor Form Overview

The <a href = "https://demos.telerik.com/blazor-ui/form/overview" target="_blank">Form for Blazor</a> allows you to generate and customize a form based on your model. You can control the component through various [parameters](https://www.telerik.com/blazor-ui/documentation/components/form/overview#form-parameters), use [default editors](https://www.telerik.com/blazor-ui/documentation/components/form/overview#automatic-generation-of-fields) or [custom ones](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview), set the [orientation](https://www.telerik.com/blazor-ui/documentation/components/form/orientation) and organize the form fields in [groups](https://www.telerik.com/blazor-ui/documentation/components/form/formgroups) and [columns](https://www.telerik.com/blazor-ui/documentation/components/form/columns).

## Creating Blazor Form

1. Add the `TelerikForm` tag to a razor file.
2. Bind the Form to data by using only one of the following two options:
   * Set the Form `Model` parameter to an object.
   * Set the `EditContext` parameter to an [`EditContext` instance](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.editcontext). The object instance from which the `EditContext` is created is important, especially when using [FormItem templates](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template) and [validation messages](https://www.telerik.com/blazor-ui/documentation/components/form/validation).
3. (optional) To enable [form validation](https://www.telerik.com/blazor-ui/documentation/components/form/validation), add the `<FormValidation>` tag. Define a validator component inside, for example the [`DataAnnotationsValidator`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.dataannotationsvalidator) that is part of .NET Core.

> caption Basic Blazor Form

<demo metaUrl="client/form/overview/" height="600"></demo>

## Form Items

There are three ways to generate fields in the Blazor Form:

* Allow the [Form to automatically generate items](https://www.telerik.com/blazor-ui/documentation/components/form/overview#automatic-generation-of-fields). In this case, each editor component type will depend on the model property type.
* [Define `FormItems`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview) manually. This allows more configuration flexibility and [changing the editor type](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview). You can also control when [changes in one Form item cause other Form items to re-render](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview#ui-rendering-inside-the-form).
* [Combine the above two options](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview#add-form-fields-to-autogenerated-ones).

### Automatic Generation of Fields

The Telerik Form can generate [editors](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview) for you based on the model fields. It can take them from both a `Model`, or the `EditContext`, whichever you provide to it. You can use [data annotation attributes](https://www.telerik.com/blazor-ui/documentation/components/form/overview#data-annotation-attributes) to validate the field values and customize the auto-generated form fields, for example set labels, visibility and editability.

The following data types are supported out-of-the box and they use the following default editors:

<style>
    .article table {
        table-layout: auto;
        word-break: normal;
    }
</style>

| Data Type                           | Default Editor                                                                                               |
| ----------------------------------- | ------------------------------------------------------------------------------------------------------------ |
| `string`                            | [Telerik TextBox](https://www.telerik.com/blazor-ui/documentation/components/textbox/overview)               |
| `int`, `double`, `float`, `decimal` | [Telerik NumericTextBox](https://www.telerik.com/blazor-ui/documentation/components/numerictextbox/overview) |
| `Enum`                              | [Telerik DropDownList](https://www.telerik.com/blazor-ui/documentation/components/dropdownlist/overview)     |
| `DateTime`                          | [Telerik DatePicker](https://www.telerik.com/blazor-ui/documentation/components/datepicker/overview)         |
| `bool`                              | [Telerik CheckBox](https://www.telerik.com/blazor-ui/documentation/components/checkbox/overview)             |

#### Customize the Automatically Generated Field Editors

You can customize the automatically generated field editors by providing the `EditorType` attribute, exposed on the `<FormItem>`, or by using the [FormItem Template](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template). The `EditorType` attribute accepts a member of the `FormEditorType` enum:

| Field data type | FormEditorType enum members                                                                       |
| --------------- | ------------------------------------------------------------------------------------------------- |
| **String**      | `FormEditorType.TextArea`<br /> `FormEditorType.TextBox`                                          |
| **Boolean**     | `FormEditorType.CheckBox`<br /> `FormEditorType.Switch`                                           |
| **DateTime**    | `FormEditorType.DatePicker`<br /> `FormEditorType.DateTimePicker`<br> `FormEditorType.TimePicker` |

<demo metaUrl="client/form/overview/example-2/" height="600"></demo>

## Data Annotation Attributes

The Telerik Form for Blazor supports validation through the `<DataAnnotationsValidator />`. This allows you to take advantage of all validation attributes from the <a href="https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations?view=net-5.0" target="_blank">data annotation attributes</a> list provided by .NET.

The Form also uses the the following attributes from the model:

* `[Display(Name = "Field Caption")]` - to get the title (caption) of the field name to render out as its label.

* `[Display(AutoGenerateField = false)]` - to skip a class member and *not* create a `FormItem` for it.

* `[Editable(false)]` - to render the built-in editor as disabled so the user cannot change its value.

You can customize the editors further through the [form items](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview). Explicit settings you provide through the parameters will take precedence over data annotation attributes.

## Form Parameters

| Parameter               | Type and Default Value                             | Description                                                                                                                                                                                                                                                                                             |
| ----------------------- | -------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `AutoComplete`          | `string`                                           | The [`autocomplete` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/autocomplete) of the `<form>` element.                                                                                                                                                                      |
| `EditContext`           | `EditContext`                                      | The [EditContext](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.editcontext) of the form.                                                                                                                                                                          |
| `Id`                    | `string`                                           | The `id` attribute of the `<form>` element. You can use it together with the [`Form` parameter of a submit button](https://www.telerik.com/blazor-ui/documentation/components/button/type). Set both parameters to the same `string` value to submit the form from a button, which is outside the form. |
| `Model`                 | `object`                                           | The object bound to the Form. It will automatically create the `EditContext` and using both parameters together is not supported.                                                                                                                                                                       |
| `ValidationMessageType` | `FormValidationMessageType` enum <br /> (`Inline`) | The validation message UI. The other options are `Tooltip` and `None`. See the [Validation](https://www.telerik.com/blazor-ui/documentation/components/form/validation) article.                                                                                                                        |

### Form Layout Customization

The [Blazor Form](https://demos.telerik.com/blazor-ui/form/overview) exposes multiple parameters that allow you to customize its layout. Besides the parameters below, the Form component also allows you to [define a completely custom layout with HTML markup and Razor components](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate).

| Parameter       | Type and Default value                     | Description                                                                                                                                                                      |
| --------------- | ------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `ButtonsLayout` | `FormButtonsLayout` enum <br /> (`Start`)  | The position and width of all Form buttons. See [Form Buttons](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/buttons).                               |
| `Columns`       | `int`                                      | The number of columns in the Form. See the [Columns](https://www.telerik.com/blazor-ui/documentation/components/form/columns) article for more information                       |
| `ColumnSpacing` | `string` (`"32px"`)                        | The amout of horizontal space between the columns. See the [Columns](https://www.telerik.com/blazor-ui/documentation/components/form/columns) article for more information.      |
| `Orientation`   | `FormOrientation` enum <br /> (`Vertical`) | The position of each label with regard to its editor. See [Orientation](https://www.telerik.com/blazor-ui/documentation/components/form/orientation) for more information.       |
| `RowSpacing`    | `string`                                   | The amout of vertical space between the rows. The default value is zero, but there is a default top margin for Form items. This parameter has effect only when `Columns` is set. |

### Styling and Appearance

The following parameters enable you to customize the appearance of the [Blazor Form](https://demos.telerik.com/blazor-ui/form/overview):

| Parameter | Type     | Description                                                                                                                          |
| --------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `Class`   | `string` | A custom CSS class for the `<form>` element.                                                                                         |
| `Size`    | `string` | Determines the size and font size of the Form editors, and the spacing between them.                                                 |
| `Width`   | `string` | Controls the width of the Form in any [CSS length unit](https://www.telerik.com/blazor-ui/documentation/common-features/dimensions). |

You can find more information for customizing the Form appearance in the [Appearance article](https://www.telerik.com/blazor-ui/documentation/components/form/appearance).

## Form Reference and Methods

Use the Form reference to get access to its `EditContext`. The Form generates this object, no matter if the component uses a `Model` or an `EditContext` parameter. You can validate the `EditContext` manually or re-attach validation when you change the model - [`FormReference.EditContext.EnableDataAnnotationsValidation(IServiceProvider serviceProvider)`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.forms.editcontextdataannotationsextensions.enabledataannotationsvalidation?view=aspnetcore-8.0#microsoft-aspnetcore-components-forms-editcontextdataannotationsextensions-enabledataannotationsvalidation\(microsoft-aspnetcore-components-forms-editcontext-system-iserviceprovider\)).

The Form also exposes a `Refresh()` method that calls `StateHasChanged()` only for the component itself.

> caption Get the Form Reference and Validate the EditContext

<demo metaUrl="client/form/overview/example-1/" height="570"></demo>

## Next Steps

* [Validate the Form](https://www.telerik.com/blazor-ui/documentation/components/form/validation)
* [Explore the Form events](https://www.telerik.com/blazor-ui/documentation/components/form/events)
* [Learn more about the Form Buttons](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/buttons)
* [Group the FormItems](https://www.telerik.com/blazor-ui/documentation/components/form/formgroups)

## See Also

* [Live Demo: Form](https://demos.telerik.com/blazor-ui/form/overview)
* [Form API Reference](https://www.telerik.com/blazor-ui/documentation/api/telerik.blazor.components.telerikform)
