I am trying to change the data-val-required
attributes of my form fields so that I get a generic "required" appear. Now I know that I can do this using the following:
@Html.TextBoxFor(model => model.Name, new { data_val_required ="required" })
But I don't want to "hard code" it into the template like that, I would prefer the model to use the required attribute set on it and then override it through js using something like this:
$('input, select, textarea').each(function () {
var element = $(this);
if (element.data('val-required')) {
element.data('val-required', 'required');
}
});
I've run this on document ready and checked the data attributes have changed (which they have) but apparently this is happening too late for the validation to take note of it. So I need to know when MVC binds it's form validation so I can run this before or how to change the error messages clientside only.
I've looked through the source of the page and just can't see where the validate gets instantiated.
For those saying change the required attribute or do it server side, I don't want to do this as if I have js disabled I display a validation summary at the top of the form which will be pretty useless if they all just say required. That is why I want to do this client side only