New to Telerik UI for .NET MAUI? Start a free 30-day trial

Implementing Custom Localization in .NET MAUI DataForm through Validation Attributes

Environment

Product DataForm for .NET MAUI
Version 7.0.0

Description

When working with the DataForm for .NET MAUI, you might need to customize the error messages displayed for validation errors. For example, you want to change the default range validation error message or required error message to a custom one.

This KB article also answers the following questions:

  • How to customize error messages in .NET MAUI DataForm when validation is implemented through attributes in your ViewModel?
  • How to change the validation error messages in DataForm through validation attributes?

Solution

When you use data annotations in your ViewModel for validation, such as "Required", for example:

public class MyViewModel
{
    [Required]
    public string MyProperty { get; set; }
}

In that case, the validation error message is provided by the attribute itself. If you have not specified your own error message explicitly, a default one is provided by the .NET framework.

You can provide your own custom error message instead:

public class MyViewModel
{
    [Required(ErrorMessage = "My custom error message.")]
    public string MyProperty { get; set; }
}

Likewise, if you have your own localization resources, you can localize the error message:

public class MyViewModel
{
    [Required(ErrorMessageResourceType = typeof(MyLocalizationResources),
              ErrorMessageResourceName = nameof(MyLocalizationResources.MyCustomErrorMessage))]
    public string MyProperty { get; set; }
}

For more information on validation attributes, refer to the ValidationAttribute Class in the Microsoft documentation.

See Also

In this article