Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

Commented Issue: Validation rules are not triggered [7117]

$
0
0
Hello,

this is my code I have setup:

public class ReleaseViewModelValidator : AbstractValidator<ReleaseViewModel>
{
public ReleaseViewModelValidator()
{
RuleFor(r => r.Name).NotEmpty().Length(1, 30).WithMessage("Name must not be longer than 30 chars.");
}
}

[FluentValidation.Attributes.Validator(typeof(ReleaseViewModel))]
public class ReleaseViewModel
{
public int ReleaseId { get; set; }
[Remote("ReleaseExists", "Release", ErrorMessage = "This name already exists.")]
public string Name { get; set; }
public DateTime CreatedAt { get; set; }
}

GlOBAL.ASAX:
FluentValidationModelValidatorProvider.Configure();

VIEW:
model ILMD.Web.Models.ReleaseViewModel

@* Remote Validation*@
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm("Create", "Release"))
{
<p class="editor-label">@Html.LabelFor(model => model.Name)</p>
<p class="editor-field">@Html.EditorFor(model => model.Name)</p>
<p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p>
}

All my unit tests for the ReleaseViewModelValidator show green light. Fine.

But less cool is that entering some live data like 31 chars or entering nothing I do not see any client side error message.

Do I still have to include something in my partial view? The ModelState is also not correct, thus I am updating my database with null values....crash...bom...bang...

Can you help me please?
Comments: Hello Jeremy, 1.) Yes mvc client-side validation is enabled in the web.config my default validation like Remote is working fine. 2.) yes I have multiple data-attributes: data-val=true, data-val-remote , data-val-remote, data-valmsg-for=Name, data-valmsg-replace=true but it seems yours is missing? I do not know as I have never seen yours before. I am anyway confused about one thing in the Global.asax: FluentValidationModelValidatorProvider.Configure(); vs. ModelValidatorProviders.Providers.Clear(); ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory())); When should I do what? I have installed fluentvalidation via nuget package: FluentVAlidation 3.4.6.0 and FluentValidation.MVC4 3.4.6.0 Should I also install FluentValidation.MVC3 ?

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>