Angular JS dynamic table generation issue with error class - angularjs

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.

Related

How to obtain values from input fields with Angular JS after ng-repeat

I'm new in AngulsrJS and I've got following code:
<form ng-if="editorCtrl.edit" class="form-horizontal" role="form" ng-submit="editorCtrl.saveEntry()">
<div class="form-group">
<div class="col-md-6" ng-repeat="(key,field) in editorCtrl.editEntry">
<label>{{key}}</label>
<input type="text" class="form-control" value={{field}} />
</div>
</div>
<!-- ng-model="editorCtrl.toSave[key]" ng-value="{{field}}" that part was in input attributes -->
<div class="form-group">
<div style="padding-left:110px">
<input type="submit" value="Update selected entry" ng-disabled="form.$invalid" class="btn btn-primary"/>
</div>
</div>
</form>
Now I need to obtain values from input fields. I tried to use ng-model="editorCtrl.toSave[key], but it's not working in a correct way.
Any suggestions how to resolve this situation ?
if you can consider no necesary the "toSave" object maybe you can use the 2-way data binding properly only using editEntry Object :
<input type="text" class="form-control" value={{field}} ng-model="editorCtrl.editEntry[key]"/>
In this way after a submit you will get editEntry with the fields modifieds (or not), here is an example
https://jsfiddle.net/pv8qrwty/1/
run the example and if you modified the fields after you press the submit button it will be displayed in your browser console
hope this help ! and sorry for my english !

how to exclude one specific ng-form in angular

I have the following form structure, how can I validate this form except for last item in ng-repeat ? it is a kind of dynamic list of inputs and last input is an empty slot, and as the user types in the input, another empty input field is added to items collection and so on, how can I tell parentForm to ignore the last item?
<div ng-form="parentForm">
<div ng-repeat="item in items" ng-form="itemForm">
<input type="text" ng-model="item.name" required/>
</div>
<input type="submit" ng-disabled="parentForm.$invalid" />
</div>
I would use special ngRepeat property $last with combination with ngRequired:
<div ng-form="parentForm">
<div ng-repeat="item in items" ng-form="itemForm">
<input type="text" ng-model="item.name" ng-required="!$last" />
</div>
<input type="submit" ng-disabled="parentForm.$invalid" />
</div>

How can i add validation ng-maxlength into ng-repeat with error message?

I have a simple code :
<div class="row" ng-repeat="aDiagnosis in diagnosisListForPrescription">
<div class="col-md-4 padding-right-zero" id={{aDiagnosis.rowIndex}}>
<input class="form-control" name="aDiagnosisName" ng-model="aDiagnosis.Name" ng-disabled="true">
</div>
<div class="col-md-4 padding-right-zero form-group" show-errors id={{aDiagnosis.rowIndex}}>
<input class="form-control" name="aDiagnosisResult" ng-maxlength="200" ng-model="aDiagnosis.Result" />
<p class="help-block" ng-if="form.aDiagnosisResult.$error.maxlength">Too Large</p>
</div>
</div>
and use $scope.form.$valid to generate the error message.
But problem is since use ng-repeat every time it finds the same name and when i want to generate second list by clicking a button ,,first error message is gone and error-message now works on the second text (obviously).
So How can i generate error-message each and every time dynamically ,,so every text form in ng-repeat,it has it's own error-message.
You can generate dynamically name attribute of your inputs in ng-repeat. For example, you can put $index (or id of your objects or whatever you want) to generate unique name for your inputs.
<div class="row" ng-repeat="aDiagnosis in diagnosisListForPrescription">
<div class="col-md-4 padding-right-zero" id={{aDiagnosis.rowIndex}}>
<input class="form-control" name="aDiagnosisName-{{$index}}" ng-model="aDiagnosis.Name" ng-disabled="true">
</div>
<div class="col-md-4 padding-right-zero form-group" show-errors id={{aDiagnosis.rowIndex}}>
<input class="form-control" name="aDiagnosisResult-{{$index}}" ng-maxlength="200" ng-model="aDiagnosis.Result" />
<p class="help-block" ng-if="form['aDiagnosisResult-' + $index].$error.maxlength">Too Large</p>
</div>
</div>
Example on plunker.

AngularJS validation message is not displayed when the field is invalid

I'm using angularJs validation in my html form. I'm using the required validation on an input, and when I erase the value there the textbox gets highlighed in red and trying to submit the pops-out the error 'Please fill out this field' But my custom error message is not displayed. Following is the code.
<form id="settingsForm" ng-submit="vm.saveSettings()" class="form">
<td style="width:30%">
<input class="userInput" name="locationName" style="width:80%" type="text" maxlength="50" data-ng-model="vm.location.locationName" required />
<label class="validationErrorText" data-ng-show="locationSettingsForm.locationName.$error.required">Location Name Required</label>
</td>
</form>
Any idea why this happens? Attached is a screenshot of how its displayed when the field is not filled.
I have created a plunker which will help you.
http://plnkr.co/edit/GVAdjcjcYczmcmzoCqoF
<form role="form" name="signUpForm" class="form-horizontal">
<div class="form-group">
<label for="url" class="col-sm-4 control-label">URL</label>
<div class="col-sm-8">
<input type="text" class="form-control" name="url" id="url" placeholder="Enter last name"
ng-model="user.lastName" required="true" ng-pattern="/(https?:\/\/)(www)?[A-Za-z0-9.\-#_~]+\.[A-Za-z]{2,}(:[0-9]{2,5})?(\/[A-Za-z0-9\/_\-.~?&=]*)*/">
<span ng-show="signUpForm.url.$dirty && signUpForm.url.$error.required">
<small class="text-danger">Please enter valid URL.</small>
</span>
<span ng-show="signUpForm.url.$dirty && signUpForm.url.$error.pattern">
<small class="text-danger">URL should have 2 to 25 characters.</small>
</span>
</div>
</div>
</form>
You can use similar validation. The name attribute should match with the condition. Here it is url.
Add
name="locationSettingsForm"
to the <form> element

how to apply required field validation in ng-repeater

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>

Resources