GitXplorerGitXplorer
c

BlazorValidationLocalization

public
3 stars
1 forks
0 issues

Commits

List of commits on branch master.
Verified
6e029b5752bddb3cc39f21ea66fc15376f717602

Create README.md

cconficient committed 5 years ago
Unverified
280075637aa934cd3579e9a83b526628560c5117

Added images

cconficient committed 5 years ago
Unverified
5f17046148a9ca118de048af6a29e3a62e15250e

Fixed index so LanguageName is updated

cconficient committed 5 years ago
Unverified
e7b6ceba5a8914c13fe20f5f69231d66eddc891c

Added FluentValidation

cconficient committed 5 years ago
Unverified
d9d2972b071e4d61d61875940de8fc574d2ab40d

Add project files.

cconficient committed 5 years ago
Unverified
0ffc29608329ec74bd2078770c6132cb55d383ef

Add .gitignore and .gitattributes.

cconficient committed 5 years ago

README

The README file for this repository.

BlazorValidationLocalization

Demonstrates localisation (sic!) or localization of validation messages for Blazor forms.

Written as an alternative answer for this StackOverflow question, it demonstrates using FluentValidation to add localization support to validation messages.

Background

Blazor supports a default DataAnnotationsValidator component, but this means closely binding the messages to the model code, and is therefore difficult to adapt for localization.

FluentValidation provides an answer since it actually has built-in localization support and the validation logic is separated from the actual model, making it easier to customize. Better still it already has localized messages for built-in components.

I created a very simple Blazor client app with a model class ProductViewModel, and added the FluentValidation library to it and created a validation class ProductViewModelValidator:

  public class ProductViewModelValidator : AbstractValidator<ProductViewModel>
  {
      public ProductViewModelValidator()
      {

          RuleFor(vm => vm.ProductCode).NotEmpty();
      }

In a regular application context FluentValidation will use the current culture - however for demonstration purposes I wanted to select the language - and FluentValidation supports that too:

    ValidatorOptions.LanguageManager.Culture = new CultureInfo(language);

I also copied Chris Sainty's validation sample to create the FluentValidationValidator component that replaces the standard DataAnnotationsValidator

For testing purposes I built a list of languages and let the user select. You select the language and click submit:

English Arabic Hindi

Acknowledgements/sources: