2

I include the set rule in the validator for my LoginModel, but when I include this RuleSet the view doesn't validate by jQuery Validate.

I use jQuery Validate Unobtrusive.

If I remove the rule the view validate correctly.

The problem is that razor doesn't include the validation rules when I include the property en the view:

@Html.TextBoxFor(m => m.Email)
@Html.ValidationMessageFor(m => m.Email)

This is the model validator with the RuleSet:

public class LoginModelValidator : AbstractValidator<LoginModel>
{
    public LoginModelValidator()
    {
        RuleSet("login", () => 
        {
            RuleFor(model => model.Email)
                .NotNull()
                .EmailAddress();

            RuleFor(model => model.Password).NotNull();
        });

        /*
        RuleSet("forgotpass", () => 
        {
            RuleFor(model => model.Email)
                .NotNull()
                .EmailAddress();
        });
        */ 
    }
}

When send the request, in the action I include this acording the documentation in 'Validator customization' section:

[CustomizeValidator(RuleSet="login")]

for validate the rule login and it work find. The problen is in the view.

edit: validate in controller

public async Task<ActionResult>  Login([CustomizeValidator(RuleSet="login")] LoginModel model)
{
    if (!ModelState.IsValid)
    {
    ...

edit: more information

I use jquery validate for validate the model. As I described before, if I not included a "ruleset" it work fine.

11
  • can we see how you used it in the controller..? Commented Apr 2, 2015 at 14:20
  • @pjobs yes, but the problem is in the view, when it try to validate by jquery validate. Commented Apr 2, 2015 at 14:23
  • It really isn't clear what you are asking. Please edit your question to include all relevant details, including the validation on the view, the call to the controller and the subsequent validation failures in the model. Commented Apr 2, 2015 at 14:32
  • I see what you are saying , so you have other fluent validations other than this ruleset, that are not being executed once you include this ruleset validation, am I right? Commented Apr 2, 2015 at 14:39
  • 1
    You may already know it, but just to make sure, validations in Ruleset do not by default propagate to client side unless you use RuleSetForClientSideMessages or you can have validations outside ruleset for clientside validations Commented Apr 2, 2015 at 15:49

2 Answers 2

2

In addition to @pjobs answer, if you need to decide about which rule set to use dynamically in action — then you can hack special FluentValidation variable, as I described here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment, it is usefull for me.
1

You may already know it, but just to make sure, validations in Ruleset do not by default propagate to client side unless you use RuleSetForClientSideMessages or you can have validations outside ruleset for clientside validations

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.