asp.net mvc - Fluent Validation doesn't validate the entire form the first time -
so i'm using fluent validation on form. when click submit , have nothing entered, validation error date of birth. if enter dob, validation first name.
why happening? can't figure out wired wrong.
my form:
@using (html.beginform()) { @html.antiforgerytoken() @html.hiddenfor(customer => customer.customerincomeinfo.customeremploymentinfomodel.employermodel.id) <!-- basic customer info --> <fieldset> <legend>customer info</legend> @html.validationsummary(false, "please correct errors , try again", new { @class = "text-danger" }) <div class="row"> <div class="col-md-6"> <dl class="dl-horizontal"> <dt>@html.labelfor(model => model.firstname)</dt> <dd>@html.editorfor(model => model.firstname, new {@class = "form-control"})</dd> </dl> </div> <div class="col-md-6"> <dl class="dl-horizontal"> <dt>@html.labelfor(model => model.dateofbirth)</dt> <dd>@html.editorfor(model => model.dateofbirth, new {@class = "form-control"})</dd> </dl> </div> </div> </fieldset> }
my fluent validation code:
public customervalidator() { rulefor(customer => customer.firstname) .length(3, 50) .notempty() .withmessage("please enter valid first name"); rulefor(customer => customer.dateofbirth).notempty().withmessage("please enter valid date of birth"); }
my model:
public class customermodel { public customermodel() { } public guid id { get; set; } [displayname("first name")] public string firstname { get; set; } [displayname("d.o.b.")] public datetime dateofbirth { get; set; } }
registering validator autofac:
builder.registertype<customervalidator>() .keyed<ivalidator>(typeof(ivalidator<customermodel>)) .as<ivalidator>();
i usingfluent validation
in project in project working same way required.i have tried code same code working fine please refer below code:
/// test code /// model class [validator(typeof (customermodelvalidator))] public class customermodel { public customermodel() {} public guid id { get; set; } [displayname("first name")] public string firstname { get; set; } [displayname("d.o.b.")] public datetime dateofbirth { get; set; } } // validator class public class customermodelvalidator: abstractvalidator < customermodel > { public customermodelvalidator() { rulefor(customer = > customer.firstname) .length(3, 50) .notempty() .withmessage("please enter valid first name"); rulefor(customer = > customer.dateofbirth).notempty().withmessage("please enter valid date of birth"); } }
hope helps you.
Comments
Post a Comment