how to apply required field validation in ng-repeater - angularjs

I have a repeater in my application and I am trying to add required field validation on the fields inside repeater. I used $index to generate id/name of the fields.
Form name is IPForm and used inside ng-class to highlight the error. Please see below code snap,
<tr ng-repeat="ClaimData in ClaimInfo">
<td>
<div ng-class="{ 'has-error' : IPForm.DateOfLoss{{$index}}.$invalid }">
<input type="text" id ="DateOfLoss{{$index}}" name="DateOfLoss{{$index}}" class="datefield form-control" ng-model="DateOfLoss"
required />
<span ng-if="IPForm.DateOfLoss{{$index}}.$invalid" class="help-block">Please enter Date of
Loss.</span>
</div>
</td>
</tr>
Problem is its not working at all. I think it is unable to evaluate IPForm.DateOfLoss{{$index}}.$invalid.

Alternatively, you can use the ng-form directive to handle your form validation which is a cleaner solution.
e.g.
<tr ng-repeat="ClaimData in ClaimInfo" ng-form="subForm">
<td>
<div ng-class="{ 'has-error' : subForm.DateOfLoss.$invalid }">
<input type="text" id ="DateOfLoss{{$index}}" name="DateOfLoss" class="datefield form-control" ng-model="DateOfLoss"
required />
<span ng-if="subForm.DateOfLoss.$invalid" class="help-block">Please enter Date of
Loss.</span>
</div>
</td>
</tr>

Related

puitting bootstrap required fields in ng-repeat

I've got a table of packages produced by ng-repeat. I'm using bootstrap validation. It works fine on pages where there's only one record requiring input, but here I'm dealing with a repeater.
<form name="packingVm.PackageForm" ng-submit="packagingVm.ShipNow()" novalidate ng-init="submitted=false">
<table>
<thead>
<tr>
<th>Package Id</th>
<th>Width</th>
</tr>
</thead>
<tbody data-ng-repeat="package in packagingVm.Packages track by $index">
<tr>
<td>{{package.Id}}</td>
<td class="col-xs-1">
<input name="Width" class="form-control input-inline input-sm" type="text" ng-model="package.Width" required valid-number />
<div class="error-message" ng-show="packagingVm.PackageForm.Width.$invalid && packagingVm.PackageForm.Width.$touched || package.submitted">
<span ng-show="packagingVm.PackageForm.Width.$error.required">Required.</span>
</div>
</td>
</tr>
</tbody>
</table>
</form>
What's happening is the rows are locked together. Getting an error on one row shows the error message on all rows.
I mean, I get why - I only have one packagingVm.PackageForm.Width, not one per row - but I don't know how to fix it.
Searching for bootstrap required ng-repeat isn't getting me much joy.
Answered here:
AngularJS required field validation in ng-repeat
Make the control name, and all references to it, dynamic, by adding {{$index}} to it, thus:
<tbody data-ng-repeat="package in packagingVm.Packages">
<tr>
<td>{{package.Id}}</td>
<td class="col-xs-1">
<input name="Width_{{$index}}" class="form-control input-inline input-sm" type="text" ng-model="package.Width" required valid-number />
<div class="error-message" ng-show="packagingVm.PackageForm.Width_{{$index}}.$invalid && packagingVm.PackageForm.Width_{{$index}}.$touched || package.submitted">
<span ng-show="packagingVm.PackageForm.Width_{{$index}}.$error.required">Required.</span>
</div>
What I can see, you are using the repeat for packagingVm.Packages but the required parts are dependent on packagingVm.PackageForm. You should have the specific required properties for each package(input). Else for all the inputs, one property of the controller is changed on which all required divs are dependent. So it shows for all the inputs.

Angular JS dynamic table generation issue with error class

Hi all I have used the following example to generate dynamic table as per my need
https://jsfiddle.net/shanidkv/vbft1sc4/
Modified the table as per my own with my own error class
<tbody>
<tr ng-repeat="personalDetail in personalDetails">
<td>
<input type="checkbox" ng-model="personalDetail.selected" /></td>
<td>
<div class="form-group" ng-class="{'has-error': rc.loginForm.needsAttention(loginForm.firstname)}">
<div class="col-sm-12">
<input type="text" name="firstname" class="form-control" ng-model="personalDetail.fname" required />
<span class="help-block" ng-show="loginForm.firstname.$error.required">Required</span>
</div>
</div>
</td>
<td>
<div class="form-group" ng-class="{'has-error': rc.loginForm.needsAttention(loginForm.lastname)}">
<div class="col-sm-12"> <input type="text" name="lastname" class="form-control" ng-model="personalDetail.lname" required />
<span class="help-block" ng-show="loginForm.lastname.$error.required">Required</span></div>
</div>
Required
Every thing works fine but when I add a second row after filling the data validation is not firing can some one help me
First click of Save
After filling data if I add another row validation for second row is not applying
The problem is, All the input box for first name/last name/email is creating for the same name. so during validation it is not able to recognized the current empty field.
Better solution, you can create a directive which will be added to the text box which will check the field validation and put the error class accordingly.

Angular using $index and $invalid in a ng-class expression

I'm using an ng-repeat directive to generate a set of forms.
I've managed to give unique names to each of the input fields using $index.
<input ng-class="{ 'has-error' : userForm.firstName-$index.$invalid }"
type="text"
class="form-control"
ng-model="passenger.firstName"
id="firstNameId-{{$index}}"
name="firstName-{{$index}}"
required
>
But, I could not get the ng-class expression to work with both the $index and $invalid statements working together.
What should be done in order to get,
ng-class="{ 'has-error' : userForm.firstName-$index.$invalid }"
to work?
Plunker is provided here
I just removed the dash all together from the input name, it is now working as intended
<form name='userForm'>
<div ng-repeat="person in people">
<input ng-class="{ 'has-error' : userForm.firstName{{$index}}.$invalid }" type="text" class="form-control" ng-model="passenger.firstName" id="firstName{{$index}}" name="firstName{{$index}}" required />
</div>
</form>
http://plnkr.co/edit/efLlNT3pUVBmENik61gU?p=preview

How to track multiple radio buttons and checkboxes created in a table using ng-repeat in Angularjs

I am new to AngularJS. I would like to know how to track which all radio buttons and checkboxes were clicked or selected on clicking the submit button. The tables rows were created using ng-repeat. The checkboxes(as seen in the image) appear when the radio button in the third column is clicked(I do an ng-show="true") and can be selected.
Attached is a mockup.
Any good design pattern or sample code as well for this use case?
Please help.
Thanks in advance.
EDIT: Adding sample code.
<tr ng-repeat="event in data">
<td><input type="radio" name={{event.performanceID}}
ng-value="yes" /></td>
<td><input type="radio" name={{event.performanceID}} ng-value="no" />
</td>
<td><input type="radio" name={{event.performanceID}}
ng-value="maybe" />
<div class="checkbox">
<label> <input type="checkbox"> Approve
</label> <label> <input type="checkbox"> Bonuses
</label>
</div></td>
<td><img
ng-src="http://google.com/images/{{ event.imageName }}.jpg"
style="height: 100px; width: 100px" /></td>
<td>
<div>
<h4>Persons Name</h4>
<h5>{{event.Ename}}</h5>
</div>
<div>
<h4>Address</h4>
<h5>XXXXXXXXXXXXXX</h5>
</div>
</td>
</tr>

AngularJs validate whole model

I have a view with some inputs in a table row. Is there a way to check if the whole model is valid without using form (can't have form in tr ) ?
<tr ng-controller="SomeCtrl">
<td>
<input type="text" ng-model="someModel.name" required="required" ng-minlength="3">
</td>
<td>
<input type="text" ng-model="someModel.x" required="required" ng-minlength="3">
</td>
<td>
<input type="text" ng-model="someModel.y" required="required" ng-minlength="3">
</td>
<td>
<button ng-click="save(someModel)">Save</button>
</td>
</tr>
In controller i want to have something like this
function ($rootScope, $scope, serive) {
$scope.save = function (someModel) {
if (someModel.$valid) {}
};
}
If you use a form and it has a name, it automatically can give you exactly what you required.
<form name="someForm">
<tr ng-controller="SomeCtrl">
<td>
<input type="text" ng-model="someModel.name" data-ng-required="true" ng-minlength="3">
</td>
<td>
<input type="text" ng-model="someModel.x" data-ng-required="true" ng-minlength="3">
</td>
<td>
<input type="text" ng-model="someModel.y" data-ng-required="true" ng-minlength="3">
</td>
<td>
<button data-ng-disabled="!someForm.$valid" ng-click="someForm.$valid && Namesave(someModel)">Save</button>
</td>
</tr>
</form>
Otherwise, there is no automagical way to do that. I guess you can write a directive which gives you all your inputs and their validators, parse them, and have a validation on the whole model, but no such thing readily exists, I believe.
Better way to do it:
ng-form name="myForm" can be used to avoid using tag and it should work - plunker.
but using ng-include will make the form unavailable to controller - plunker , explanation. A work around for this issue is to pass the form as parameter to the controller method - plunker

Resources