5

I'm using FluentValidation to validate my models both client side and server side. I am on the latest version of :

FluentValidation.MVC5

at the time of writing, which is

5.5.0.0

I have the following validator, simplified:

public class MyViewModelValidator : AbstractValidator<MyViewModel>
    {
        public MyViewModelValidator()
        {
                RuleFor(x => x.Email)
                    .EmailAddress().WithLocalizedMessage(() => MyResources.Validation_Email_NotValidAddress)
                    .NotEmpty()
                    .WithLocalizedMessage(() => MyResources.Validation_Email);
        }
    }

Client side it seems to do some basic validation, such as it will not accept anything without text either side of the '@' symbol, however it will accept something like test@test.

The problem arises when I post this data, I have the following in my Controller:

    if (!ModelState.IsValid)
        throw new Exception("Model validation error");

This sees the model as invalid due to the test@test email address and throws an error. So it appears my front end validation is more lax that my server side validation.

Having referred to the documentation, it does state that the Email() method is supported clientside, however there does seem to be some disparity between this server side, and what is being rendered to the front end.

https://fluentvalidation.codeplex.com/wikipage?title=mvc

How can I ensure the client side validation is as thorough as the server side with the email.

JsonStatham
  • 9,770
  • 27
  • 100
  • 181

2 Answers2

4

As Evgeny touched on above, I eventually opted to use the Matches rule, as this is one of the validators which is supported on the client.

   RuleFor(x => x.Email)
                .Matches(@"^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$").WithLocalizedMessage(() => MyResources.Validation_Email_NotValidAddress)              
                .NotEmpty() // <-- and cant be empty
                .WithLocalizedMessage(() => MyResources.Validation_Email);

The above Regex I believe is used by ASP.NET for the RegularExpressionValidator. And works well for my requirement.

JsonStatham
  • 9,770
  • 27
  • 100
  • 181
2

Client-side rule for email input (decorated with data-val-email attribute) defined in jquery.validate plugin core (search on page by 'email' word and you find RegExp)

Server-side rule for email property defined in Fluent Validation EmailValidator class (also RegExp)

In terms of life-cycle these projects have nothing common: in any new version release internal regexp can became more/less strict in both projects simultaneously, and if your goal is to have same rule on client and server and get it work correctly regardless plugin/library version updates — my recommendation is to replace current EmailAddress() rule with Match() rule and specify regexp, that satisfies you domain model needs, explicitly.

UPDATE:

Choosing proper regexp is another topic, that is widely described here

Community
  • 1
  • 1
David Levin
  • 6,573
  • 5
  • 48
  • 80