I have a form with 3 input fields. But I want to use the form also for other pages. Therefore I have reduced the form. At the beginning I've defined the view as follows:
View:
<form class="form-horizontal" name="editForm" novalidate>
<div ng-repeat="elements in form">
<div class="form-group-sm has-feedback" ng-repeat="el in elements.items" ng-class="{ 'has-error' : hasError(editForm), 'has-success' : hasSuccess(editForm) }">
<label class="control-label" for="{{el.id}}">{{el.label}}</label>
<input type="{{el.fieldType}}"
class="form-control"
placeholder="{{el.label}}"
name="{{el.name}}"
id="{{el.id}}"
ng-model="selected[el.model]"
ng-disabled="{{el.disabled}}"
ng-pattern="el.pattern"
ng-required="{{el.required}}"
/>
<p class="help-block" ng-show="editForm.{{el.name}}.$error.required && editForm.{{el.name}}.$touched">Field is required.</p>
<p class="help-block" ng-show="editForm.{{el.name}}.$error.pattern">Thats the pattern error message.</p>
</div>
</div>
</form>
I want to outsource the error messages / validation in a function. The error messages in the p-Tag don't work.
I've tried this in my Ctrl:
$scope.hasError = function (form) {
return form.Firstname.$invalid && form.Firstname.$dirty;
}
$scope.hasSuccess = function (form) {
return form.Firstname.$valid;
}
That is only for the inputfield Firstname. But how can I define this dynamically for all fields (firstname, lastname, age)?