Articles in this section

How to validate DataForm on page loading in .NET MAUI DataForm (SfDataForm)?

The Syncfusion® .NET MAUI DataForm (SfDataForm) allows you to validate a DataForm on page loading through the Loaded event handler. This article demonstrates how to perform validation when the page is initialized.

C#

Define the model class properties with appropriate validation attributes to specify error messages.

[Display(Name = "First Name")]
[Required(AllowEmptyStrings = false, ErrorMessage = "Please enter your first name")]
public string FirstName { get; set; }

[Display(Name = "Last Name")]
public string LastName { get; set; }

[DataType(DataType.Password)]
[DataFormDisplayOptions(ColumnSpan = 2, ValidMessage = "Password strength is good")]
[Required(ErrorMessage = "Please enter the password")]
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{8,}$", ErrorMessage = "A minimum 8-character password should contain a combination of uppercase and lowercase letters.")]
public string Password { get; set; }

[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
[Required(ErrorMessage = "Please confirm the password")]
[DataFormDisplayOptions(ColumnSpan = 2)]
public string RetypePassword { get; set; }

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

[Required(ErrorMessage = "Please select your city")]
public string City { get; set; }

C#

Attach the Loaded event handler in the behavior class to trigger validation upon loading the SfDataForm.

private SfDataForm dataForm;

protected override void OnAttachedTo(ContentPage bindable)
{
   base.OnAttachedTo(bindable);
   this.dataForm = bindable.FindByName<SfDataForm>("dataForm");
   if (this.dataForm != null)
   {   
       this.dataForm.Loaded += OnLoaded;
   }
}

C#

Use the OnLoaded event to validate the DataForm using the Validate method.

private void OnLoaded(object sender, EventArgs e)
{
   this.dataForm.Validate();
}

Download the complete sample from GitHub.

Output:

ValidateOnLoad.png

Conclusion:

I hope you enjoyed learning how to validate the SfDataForm on page loading in the .NET MAUI DataForm.

Refer to our .NET MAUI DataForm feature tour page for its other groundbreaking feature representations. You can also explore our .NET MAUI DataForm documentation to understand how to present and manipulate data.

For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI DataForm and other .NET MAUI components.

Please let us know in the following comments section if you have any queries or require clarification. Contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied