DataForm StringLength Not Working

I'm trying to set the field length in my Syncfusion sfDataForm control and I'm setting it in XAML.

Here is the code:

 [Display(GroupName = "Address")]
 [DataFormDisplayOptions(RowOrder = 2, ItemsOrderInRow = 1)]
 [StringLength(2, ErrorMessage = "State Abbr should not exceed 2 characters")]
 public string State { get; set; }

But this has no effect on the displayed form.

How/where is the correct place to set the StringLength in .


1 Reply

VM Vidyalakshmi Mani Syncfusion Team November 14, 2023 01:56 PM UTC

Hi Frederick,


The `StringLength` attribute does not restrict the input length. Instead, it generates only an error message if the length of the input string exceeds the specified limit. If you want to strictly limit the maximum length of the input string to 2 characters, we suggest using the `MaxLength` property instead. Please see the following code snippet for your reference:


 

dataForm.GenerateDataFormItem += OnGenerateDataFormItem;    

 

private void OnGenerateDataFormItem(object sender, GenerateDataFormItemEventArgs e)

{

    if (e.DataFormItem != null)

    {

        if (e.DataFormItem.FieldName == "State" && e.DataFormItem is DataFormTextItem state)

        {

 

            state.MaxLength = 2;

 

        }

    }

}

 


We've also prepared a simple sample that demonstrates the usage of the `MaxLength` property. Please find the attached sample for your reference.


We hope this helps you. Please let us know if you need any further assistance.



Attachment: MauiDataForm_85a58ec0.zip

Loader.
Up arrow icon