I am using ng-form as a parent form and child mg-forms for validation with ng-repeat.
<div ng-form="form">
<div ng-repeat="field in fields">
<div ng-form="subform"><input...></div>
</div>
</div>
And validating like this:
if ($scope.form.subform.$invalid && !$scope.form.subform.$error.required) {
// Do something...
}
(this is very simplified example, I have more different subforms for different input types and with different names, e.g. input[text] is named as a TextForm, input[numeric] is NumericForm etc.)
Everything works as expected if there is only on field. But if ng-repeat generates multiple fields, validation triggers only the last subform, others gets ignored.
Is there a way of cycling through all subforms to check if one of them is invalid?
Also, I am marking all unfilled required fields like this:
if ($scope.form.$error.required) {
angular.forEach($scope.form.$error.required,
function (object, index) {
$scope.form.$error.required[index].$setDirty();
}
);
}
So if my fields are done like this:
....ng-form="TextForm" ng-class="{ 'has-error': TextForm.$dirty && TextForm.$invalid }"....
And it marks all the subforms even if there are many of the same ones with the same name.
Maybe I could do something similar with invalid fields? Though tried many things, nothing worked...
ng-form
name? if it is, then no. They will still work as it is only used as a reference to access validation values for eachng-form
context. – LyndalyndesubForms
in a controller, simply use them in a declarative way, can you provide a use case wherein it maybe relevant. @Julius, Can you tell me your use case, why are you iterating through allngForms
perhaps there is another way to solve your problem. – Lyndalyndedisabled
directive to disable the submit button whenform.$invalid
is true. Second, passform
as a parameter for yourng-submit
callback,ng-submit(form)
and then in your controller you could do,if(form.$invalid) {/*your code */}
– Lyndalynde.form-group
below the input elements as.help-block
s? If that is the case then simply useng-show
in each of those.help-block
s within thesubForm
's context. – Lyndalynde