-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Nested Forms validation #5858
Comments
This seems like it should be pretty trivial to implement. Care to submit a patch? |
What would be an appropriate name for this attribute ? ng-propagate ? ng-isolate ? Any suggestions ? |
Probably we would want to prevent the registration of the form in its parent form altogether. I'm thinking: a |
related #5037 |
I wrote a temporary solution, waiting the fix from angular. Here is the http://jsfiddle.net/gikoo/qNrFX/
You can use it like that:
|
Another proposed solution: #8917 |
👍 |
1 similar comment
+1 |
Child forms propagate always their state to its parent form. Following the pattern of ngModelOptions a new optional attribute is defined for forms, ngFormOptions that will allow to define now if the form is isolated from its parent. In the future, if more options are needed this new attribute may be the place to define them. Options are exposed in the controller, but the isolated property is read only when the NgFormController is executed, so the behavior won't change if its value is changed later. It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-options="{isolated:true}"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
+1 also: #8917 |
Implementation done by this guy in SO (link below), seems ideal (in functionality) to me, as it preserves most of the isolated forms functionality. In my project I am using this code as a base for a custom directive, actually the only change I made was add $setPristine. |
@rodrigopedra is that solution working for you in AngularJS 1.3? |
yep. I did a small change, but just for my use case. Here is the directive I am using to isolate the form:
If you want I can send you the snippet for the component I am using isolated, it is a list of phones/emails that the user can add/remove in an internal form. The component syncs an array with the outer form through |
Child forms propagate always their state to its parent form. Following the pattern of ngModelOptions a new optional attribute is defined for forms, ngFormOptions that will allow to define now if the form should be considered as 'root', therefore preventing the propagation of its state to its parent. In the future, if more options are needed this new attribute ngFormOptions may be the place to define them. Options are exposed in the controller, but the isolated property is read only when the NgFormController is executed, so the behavior won't change if its value is changed later. It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-options="{root:true}"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
Child forms propagate always their state to its parent form. Following the pattern of ngModelOptions a new optional attribute is defined for forms, ngFormOptions that will allow to define now if the form should be considered as 'root', therefore preventing the propagation of its state to its parent. In the future, if more options are needed this new attribute ngFormOptions may be the place to define them. Options are exposed in the controller, but the isolated property is read only when the NgFormController is executed, so the behavior won't change if its value is changed later. It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-options="{root:true}"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
Child forms propagate always their state to its parent form. Following the pattern of ngModelOptions a new optional attribute is defined for forms, ngFormOptions that will allow to define now if the form should be considered as 'root', therefore preventing the propagation of its state to its parent. In the future, if more options are needed this new attribute ngFormOptions may be the place to define them. Options are exposed in the controller, but the isolated property is read only when the NgFormController is executed, so the behavior won't change if its value is changed later. It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-options="{root:true}"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
I wanted to reach a slightly different result from the one described by this issue: i.e., I just wanted to detach some controls from the containing form. This directive does the job, simply removing the .directive('evictForm', function() {
return {
restrict : 'A',
link: {
pre: function (scope, iElement) {
iElement.data('$formController', null);
}
}
};
}) I think it might apply to subforms as well, so I'm just posting it here. |
Child forms propagate always their state to its parent form. A new optional attribute ngFormTopLevel is defined for forms that will allow to define now if the form should be considered as 'top leve', therefore preventing the propagation of its state to its parent. I It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-top-level="true"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
Child forms propagate always their state to its parent form. A new optional attribute ngFormTopLevel is defined for forms that will allow to define now if the form should be considered as 'top leve', therefore preventing the propagation of its state to its parent. I It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-top-level="true"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
Child forms propagate always their state to its parent form. A new optional attribute ngFormTopLevel is defined for forms that will allow to define now if the form should be considered as 'top leve', therefore preventing the propagation of its state to its parent. I It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-top-level="true"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
Child forms propagate always their state to its parent form. A new optional attribute ngFormTopLevel is defined for forms that will allow to define now if the form should be considered as 'top leve', therefore preventing the propagation of its state to its parent. I It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-top-level="true"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
Child forms propagate always their state to its parent form. A new optional attribute ngFormTopLevel is defined for forms that will allow to define now if the form should be considered as 'top leve', therefore preventing the propagation of its state to its parent. I It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-top-level="true"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
Child forms propagate always their state to its parent form. A new optional attribute ngFormTopLevel is defined for forms that will allow to define now if the form should be considered as 'top leve', therefore preventing the propagation of its state to its parent. I It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-top-level="true"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
@alessiodm your solution breaks form from updating itself. |
+1 |
…tion password changes angular/angular.js#5858
+1 Using @91K00 solution also! |
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
…tion password changes angular/angular.js#5858
Child forms propagate always their state to its parent form. A new optional attribute ngFormTopLevel is defined for forms that will allow to define now if the form should be considered as 'top level', therefore preventing the propagation of its state to its parent. I It maybe used like this: <ng:form name="parent"> <ng:form name="child" ng-form-top-level="true"> <input ng:model="modelA" name="inputA"> <input ng:model="modelB" name="inputB"> </ng:form> </ng:form> Closes: angular#5858
First of all, thank you to @91K00 and @gonzaloruizdevilla for trying to fix the problem. lets assume this structure:
neither the ngFormTopLevel or @91K00 solution can handle this. tl;dr : Test case 1: Test case 2: Test case 3: and the other problem is the P tag inside the nested form does not show when the Input01 is invalid. |
Hi rSafari, |
Thank you @91K00 for your answer. of course i have ng-model for my inputs. i just forgot to add it in this structure ... ( i updated my post) the main problem is that i have to validate the nested form before i can submit the parent form. it means i cant submit the parent form until i add something valid to the nested form inputs. by the way, the {{X2.Input01.$error}} is empty and don't have $dirty or $invalid object in it. |
stackoverflow related topic : |
This solution no longer works (tested in v1.6.2) due to an exception when copying the original form controller.
Any suggestions for a workaround? |
also not working on v1.6.3 |
why do need to copy the form Controller anyway? It should be enough to simply call parent.$removeControl(self) in the directive. If the form does not have a parent form controller, it doesn't propagate change upwards. |
Here is working code for angular >=1.6.2, based recent comment. https://github.com/momega/isolate-form |
Isolate forms aren't in scope for core because the logic required is too heavy. See #10193 (comment) You can detach form controls, however. |
It would be great to add some optional attribute to form element or div with ng-form attribute which will say that validation errors of current form shouldn't affect validation of parent form.
Here is what I mean:
Result:
parentForm.$valid -> true, childForm.$valid -> false
Same logic may be applied for
parentForm.$pristine -> true, childForm.$pristine -> false
The text was updated successfully, but these errors were encountered: