Validate controls within ng-repeat: textbox and textarea

1.1k views Asked by At

I am using Angular js, in which i have a textbox outside and an ng-repeat containing textbox and textarea. I want to check if the fields contain value when submit button is clicked. I am able to achieve the functionality for controls outside ng-repeat, but not sure how to achieve required field validation within ng-repeat, when submit button is click. Below is the existing code:

<form name="mainForm" id="createForm" ng-submit="mainForm.$valid && add()" novalidate>
        <div ng-controller="testController" ng-init="init()">    

    <div>
                        <label>Name :</label>
                    </div>
                    <div>
                        <input type="text" maxlength="150" required ng-model="testName" name="testName" />
                    </div>
                </div>
                <span style="color:red" ng-show="submitted == true && mainForm.testName.$error.required">Name is required</span>
                <br />
                <div class="row">
                    <div class="form-group ">
                        <label>Language</label>
                        <label>Title</label>
                        <label>Description</label>
                    </div>
                </div>
                <div class="row">
                    <div>
                        <div ng-repeat="Descriptions in testsWithDescription ">
                            <div>
                                <label ng-model="Descriptions.Language">{{Descriptions.Language}}</label>
                            </div>
                            <div>
                            <input type="text" maxlength="150" name="titleValidate[]" ng-model="Descriptions.Title" />
                            </div>
                            <div>
                                <textarea maxlength="500" name="descriptionValidate[]" noresize ng-model="Descriptions.Description"></textarea>
                            </div>
                            <div class="form-group col-md-1">
                                <a style="cursor:pointer"><img ng-src="{{DeleteIcon_url}}" alt="delete image" ng-click="($index == !selectedDeleteIcon) ||testsWithDescription.splice($index,1)" ng-class="{'disabled': $first}" /> </a>

                            </div>
                        </div>
                    </div>
                </div>

    <input type="submit" value="Submit" ng-click="submitted=true"/>

How to use required field validation for controls within ng-repeat using angular js? Thanks

1

There are 1 answers

0
Manu Obre On BEST ANSWER

You could use $index to track the name of the different inputs in your ng-repeat.

<div ng-repeat="Descriptions in testsWithDescription ">
     <input type="text" 
            maxlength="150" 
            name="titleValidate_{{$index}}" 
            ng-model="Descriptions.Title" 
            required />
</div>

You can now use the common validations from AngularJS like you already did: mainForm.$valid.