I'm attempting to wire up an adapter to enable the markup that ASP.Net MVC emits for client-side validation to work within AngularJS, and I've encountered an interesting snag. If I dynamically add the required attribute via a directive compile function:
var myApp = angular.module('myApp', []).directive('valRequired', function() {
return {
compile: function (element) {
element.attr('required', 'required');
return function() { /* other custom logic here */ }
}
};
});
The select element won't validate as required. It only appears to be a problem when dynamically adding the attribute (jsFiddle).
Clarification: I'd like to use MVC's @Html.TextBoxFor(...) as-is. For a DataAnnotations-based model, the data-val-* attributes it emits contain information on which validations to run and what the error messages should be. I'm not looking for assistance wiring up the error messages, I just need to be able to wire up a directive that tells the input, select, etc. to use the required validation.
data-val-requiredattribute using things like@Html.TextBoxFor(...)and the like. I'd like to automatically transform them into their angularjs equivalents and add some wiring to plumb the MVC-emitted validation messages into the app as well.ng-requirecan be written asdata-ng-requireand will still be parsed by angular.data-val-required) you can create your own@Htmlextension methods like@Html.AngularTextBoxFor(...)and save the client-side performance penalty. I've done this in my app. If you're interested I could write this up further at some point.HtmlHelperextension method still doesn't solve the entire problem: I also need to wire up the validation messages so that@Html.ValidationMessageFor(...)works properly as well.