For part of my app, the server dictates all the fields of a form, including
validation. Server returns back an array of the following datatypes:
function IntField(name, required, minValue, maxValue) {
this.fieldType = 'Int';
this.name = name;
this.required = required;
this.minValue = minValue;
this.maxValue = maxValue;
}
function StringField(name, required, maxLength, regexPattern) {
this.fieldType = 'String';
this.name = name;
this.required = required;
this.maxLength = maxLength;
this.regexPattern = regexPattern ? regexPattern : null;
}
Using ng-repeat, I create all the input fields:
<div ng-repeat="field in fields">
<label>{{field.name}}</label>
<input type="text" data-driven-field field="field"/>
</div>
I would like to drive validation using a directive. So far I have:
common.directive('dataDrivenField', function() {
return {
template: '<input required="field.required" />',
restrict: 'A',
replace: true,
scope: {
field: '='
},
link: function(scope, element) {
if (scope.field) {
if (scope.field.fieldType === 'String') {
element.attr('type', 'text');
} else if (scope.field.fieldType === 'Int') {
element.attr('type', 'number');
element.attr('min', scope.field.minValue);
element.attr('max', scope.field.maxValue);
}
}
},
};
});
Since every field has a required attribute, I can safely put that in the
template for compilation. But different field types have different
attributes. In the link phase, I set HTML5 form attributes, but this isn't
doing angular validation.
What I'd *like* to be able to do is somehow reuse the existing Angular
text/number validators, but I can't figure out a way to reference them
here. Something like:
link: function(scope, element) {
if (scope.field) {
if (scope.field.fieldType === 'String') {
...
applyAngularStringValidation(scope.field.maxLength,
scope.field.regexPattern);
} else if (scope.field.fieldType === 'Int') {
...
applyAngularIntValidation(scope.field.minValue,
scope.field.maxValue);
}
}
},
Is there any way to get reference to their validation logic at link time?
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/groups/opt_out.