I created a new RequiredAttribute extention called "RequredWhen".
It works well on textboxes but not on select elements. For some reason those only validate on postback instead on client side. The "Required" drop downs validate well client side so why aren't the "RequiredWhen" attributes failing to validate?
When you add this attribute
[Required]
You get this markup
<select class="customSelect input-validation-error" data-val="true" data-val-number="The field x must be a number." data-val-range="Please select the correct range" data-val-range-max="5" data-val-range-min="0" data-val-required="The x field is required." id="x" name="x"></select>
This doesn't happen you add the custom attribute. In the markup all the validation related attributes are missing from the select element (like data-val, etc) but they are applied on input fields.
Is there something additional I need to do to force validation to work on select elements? Also the GetClientValidationRules does not include those elements when I debug. I'm sure that's part of the problem.
My attribute extends RequiredAttribute which works on select elements so I am not sure why they are excluded from my implementation. Any ideas?
Here's my javascript which works well on other elements.:
jQuery.validator.unobtrusive.adapters.add('requiredwhenchecked', [ 'otherproperty', 'checkedproperty', 'otherpropertyvalue' ],function (options) {
// simply pass the options.params here
options.rules['requiredWhenChecked'] = options.params;
}
);
jQuery.validator.addMethod('requiredWhenChecked', function (value, element, params) {
return true;
}, '');
There's something that makes this all work well, except for "select" attributes.
I think my problem might be related to this and this case which don't seem to have a solution yet
UPDATE There IS no solution but there is a workaround and I found it here! Good luck to whoever has this nasty problem