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. Specifically, you want to change the default range validation 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

To customize the error messages in the DataForm component, create a custom localization manager by extending the TelerikLocalizationManager class. Override the GetString method to return custom messages for specific keys.

Here is how to implement a custom localization manager:

public class CustomTelerikLocalizationManager : TelerikLocalizationManager
{
    public override string GetString(string key)
    {
        if (key == "DataForm_RangeValidationError")
        {
            return "My error message";
        }

        return base.GetString(key);
    }
}

After implementing the custom localization manager, assign it to the TelerikLocalizationManager.Manager property before initializing the components:

TelerikLocalizationManager.Manager = new CustomTelerikLocalizationManager();
InitializeComponent();

Using Validation Attributes in Your ViewModel

Specify validation rules using data annotations:

[Required]
[Range(0, 1000000, ErrorMessage = "My error message")]
public int? Population
{
    get => population;
    set => UpdateValue(ref population, value);
}

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

See Also

In this article