jquery - MVC custom DataAnnotation (RequiredAttribute) not applied on select elements -
i created new requiredattribute extention called "requredwhen".
it works on textboxes not on select elements. reason validate on postback instead on client side. "required" drop downs validate client side why aren't "requiredwhen" attributes failing validate?
when add attribute
[required]
you markup
<select class="customselect input-validation-error" data-val="true" data-val-number="the field x must number." data-val-range="please select correct range" data-val-range-max="5" data-val-range-min="0" data-val-required="the x field required." id="x" name="x"></select>
this doesn't happen add custom attribute. in markup validation related attributes missing select element (like data-val, etc) applied on input fields.
is there additional need force validation work on select elements? getclientvalidationrules not include elements when debug. i'm sure that's part of problem.
my attribute extends requiredattribute works on select elements not sure why excluded implementation. ideas?
here's javascript works on other elements.:
jquery.validator.unobtrusive.adapters.add('requiredwhenchecked', [ 'otherproperty', 'checkedproperty', 'otherpropertyvalue' ],function (options) { // pass options.params here options.rules['requiredwhenchecked'] = options.params; } ); jquery.validator.addmethod('requiredwhenchecked', function (value, element, params) { return true; }, '');
there's makes work well, except "select" attributes.
i think problem might related this , this case don't seem have solution yet
update there no solution there workaround , found here! luck whoever has nasty problem
there nothing worthy of mentioning attribute class itself, extends requiredattribute , should work similarly, shouldn't it?
no, of course not. required attribute has corresponding requiredattributeadapter
associated in metadata provider. if want client validation work custom attribute should either write custom adapter (by deriving dataannotationsmodelvalidator<requiredwhen>
) or having requiredwhenattribute
implement iclientvalidatable
interface.
you may take @ source code of mvc foolproof validation
validation framework see how done. have written example here
.