0

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

3
  • 1
    Please show your code. Commented Apr 25, 2013 at 11:48
  • Due to their nature select elements need no further validation other than required. Have you written any code to intercept the RquiredWhen on a select element? Commented Apr 25, 2013 at 11:51
  • when you add the required attribute all this stuff get added to the select automatically. Like this: <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 happens automatically using the [Required] attribute. However it doesn't happen with my attribute that extends the attribute above Commented Apr 25, 2013 at 12:46

1 Answer 1

1

There is nothing worthy of mentioning about the attribute class itself, it extends RequiredAttribute and should work similarly, shouldn't it?

No, of course not. The Required attribute has a corresponding RequiredAttributeAdapter associated to it in the metadata provider. If you want client validation to work for your custom attribute you should either write a custom adapter (by deriving from DataAnnotationsModelValidator<RequiredWhen>) or by having your RequiredWhenAttribute implement the IClientValidatable interface.

You may take a look at the source code of the MVC Foolproof Validation validation framework to see how this is done. I have written an example here.

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

1 Comment

It implements IClientValidatable and another similar attribute I created called "RequiredIf" works as intended. I think the problem is elsewhere...

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.