Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 2.64 KB

custom-localization-dotnet-maui-dataform.md

File metadata and controls

75 lines (59 loc) · 2.64 KB
title description type page_title slug tags res_type ticketid
Implementing Custom Localization in .NET MAUI DataForm through Validation Attributes
Learn how to create and apply custom localization values for the .NET MAUI DataForm component to customize validation error messages.
how-to
Customizing Localization for Validation Messages in .NET MAUI DataForm when using data annotations
custom-localization-dotnet-maui-dataform
dataform, .net maui, localization, validation, custom error message
kb
1663273

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