Data annotations in .NET MAUI DataForm (SfDataForm)

3 Jan 20234 minutes to read

The data form supports the following attribute to handle the data, and these attributes can be accessed using the System.ComponentModel.DataAnnotation assembly.

Display attribute

Property Details

Name

Specifies the label text.

GroupName

Specifies the group name that identifies the fields in the data form.

ShortName

Specifies the label text. It takes higher priority than Name.

AutoGenerateField

Specifies whether the field should be auto-generated or not.

ResourceType

Specifies the Resources File (.Resx), which is used to localize the Display attribute of Name, ShortName, GroupName, Prompt and ItemsSource values.

Prompt

Specifies watermark text for the editor.

Order

Specifies the order of field in the data form.
[Display(Name = "First Name", GroupName = "Name", Prompt="Enter your name")]
public string FirstName { get; set; }

Validation attribute

Property Details

MinLength

Specifies the required minimum input string length.

MaxLength

Specifies the required maximum input string length.

Required

Specifies the required data field value.

StringLength

Specifies the required string length.

EnumDataType

Specifies the enum type for the data field.
[MinLength(5, ErrorMessage = "Password length must be greater than 5 characters")]
public string Password { get; set; }
[MaxLength(20, ErrorMessage = "Maximum password length should be less than 20")]
public string Password { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Name is required")]
public string Name { get; set; }
[StringLength(20, ErrorMessage = "Name should not exceed 20 characters")]
public string Name { get; set; }
[EnumDataType(typeof(Gender), ErrorMessage = "Please select Gender")]
public Gender Gender { get; set; }

Bindable attribute

It specifies whether the field should be auto-generated or not. If bindable is set to false, the field will not be auto-generated.

[Bindable(false)]
public string Name { get; set; }

Editable attribute

It specifies whether the data field is editable or not.

[Editable(false)]
public string Name { get; set; }

ReadOnly attribute

It specifies whether the data field is read only or not.

[ReadOnly(true)]
public string Name { get; set; }

NOTE

The ReadOnlyAttribute takes higher priority than EditableAttribute

DataType attribute

It specifies the data type for the field.

The Supported data types are Text, MultilineText, Date, DateTime and Time.

[DataType(DataType.MultilineText)]
public string Address { get; set; }

Custom attribute

The data form supports the following custom attribute, and these attributes can be accessed using the Syncfusion.Maui.DataForm assembly.

DataFormDisplayOptions attribute

Property Details

RowSpan

Specifies the row span for the data form item.

ColumnSpan

Specifies the column span for the data form item.

ValidMessage

Specifies the positive message to be shown when validation is passed.

ShowLabel

Specifies whether the label should be visible or not.

RowOrder

Specifies the row order of field in the data form

ItemsOrderInRow

Specifies the ItemsOrderInRow property to display multiple editors in a single row. Two items with the same RowOrder property value are placed in this row according to their ItemOrderInRow property values.
[DataFormDisplayOptions(Rowspan = 2, ColumnSpan = 2, RowOrder = 1, ItemsOrderInRow = 0)]
public string Name { get; set; }

DataFormValueConverter attribute

Specifies the Converter type, which converts the original value in a different format or as a different value.

[DataFormValueConverter(typeof(StringToDateConverter))]
public string Name { get; set; }

DateFormDateRange attribute

Property Details

MaximumDate

Specifies the maximum date that can be selected in the date editor.

MinimumDate

Specifies the minimum date that can be selected in the date editor.

DisplayFormat

Specifies the format of the

MaximumDate

and

MinumumDate

used in attribute.
[DataFormDateRange(DisplayFormat="yyyy/mm/dd", MaximumDate ="2022/07/01", MaximumDate ="2022/07/07")]
public DateTime EventDate { get; set; }

NOTE

View sample in GitHub