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.