angularjsvalidationangularjs-ng-form

validate nested forms without affecting the parent form


There is a validation problem in the nested forms in angularjs 1.5 and there is an issue in github about it.

but 2 people in that topic offer the solution and one of them has open its way to the angularjs core which is ngFormTopLevel directive, and the other one offered by a user called isolate-form.

but neither of them can handle this situation and not working for me ... at least !

lets assume this structure:

<ng-form name="X1" novalidate>

    <ng-form name="X2" novalidate isolate-form>

        <input name="Input01" ng-model="input1" required />
        <p ng-show="X2.Input01.$touched && X2.Input01.$invalid">input is not valid</p>

        <input name="Input02" ng-model="input2" required />

        <input type="button" id="ButtonX2" value="Submit Nested Form" ng-disabled="X2.$invalid" />

    </ng-form>

<input name="Input03" ng-model="input3" required ng-minlength="5" />

<input type="button" id="ButtonX1" value="Submit Nested Form" ng-disabled="X1.$invalid" />

</ng-form> 

tl;dr : ButtonX1 is dependent on nested form validation and it shouldn't !


Test case 1:

Step 1: Fill input3 with any text and more than 5 character.

Expected: ButtonX1 should be enable.

Result: ButtonX1 still disabled.


Test case 2:

Step 1: Fill input1 with any text.

Step 2: Fill input2 with any text.

Expected: ButtonX2 should be enable.

Result: ButtonX2 is enabled.


Test case 3:

Step 1: Fill input3 with any text and more than 5 character.

Step 2: Fill input1 with any text.

Step 2: Fill input2 with any text.

Expected: ButtonX1 and ButtonX2 should be enable.

Result: ButtonX1 and ButtonX2 is enabled.


and the other problem is the P tag inside the nested form does not show when the Input01 is invalid. I tried both the isolateForm and the ngFormTopLevel but both of them have this problem.


Solution

  • I think this is the solution as per your requirement.

    <ng-form name="X1" novalidate>
    
            <ng-form name="X2" novalidate isolate-form>
    
                <input name="Input01" ng-model="input1" required />
                <p ng-show="X2.Input01.$invalid && X2.Input01.$touched">input is not valid</p>
    
                <input name="Input02" ng-model="input2" required />
    
                <input type="button" id="ButtonX2" value="Submit Nested Form" ng-disabled="X2.$invalid" />
    
            </ng-form>
    
            <input name="Input03" ng-model="input3" required ng-minlength="5" />
    
            <input type="button" id="ButtonX1" value="Submit Nested Form" ng-disabled="X1.Input03.$invalid" />
    
        </ng-form>