Validation message issue in AngularJS - angularjs

I am using the below code for validating required, min length and max length, which is giving all three messages. Could you please tell how to control based on the user input.
<form name="Textvaluepair" novalidate>
<h4>New Network</h4>
<div class="form-group" ng-class="{ 'has-error': Textvaluepair.name.$touched && Textvaluepair.name.$invalid }">
<label>Name</label>
<input type="text" name="name" class="form-control"
ng-model="networkModel.name"
ng-minlength="5"
ng-maxlength="10"
required>
<div class="help-block" ng-messages="Textvaluepair.name.$error" ng-if="Textvaluepair.name.$touched">
<p ng-message="minlength">Your name is too short.</p>
<p ng-message="maxlength">Your name is too long.</p>
<p ng-message="required">Your name is required.</p>
</div>
</div>
<div class="form-group">
<button type="submit" ng-click="Submit()">Submit</button>
</div>
</form>

Please try to use this code
<span ng-show="Textvaluepair.name.$error.required" class="help-block">Required</span>
<span ng-show="Textvaluepair.name.$error.minlength" class="help-block">Min Length</span>
<span ng-show="Textvaluepair.name.$error.maxlength" class="help-block">Max Length</span>

change your code like this
<div class="help-block" ng-repeat="(key, value) in Textvaluepair.name.$error" ng-if="Textvaluepair.name.$touched">
<p ng-if="key == 'minlength'">Your name is too short.</p>
<p ng-if="key == 'maxlength'">Your name is too long.</p>
<p ng-if="key == 'required'">Your name is required.</p>
</div>

Related

ng-required not working for enabled fields from disabled state

I am facing an issue with the Angular validation for fields which disables/enables on checkbox check.
The scenario in which it is failing is stated below:
Checkout page is having address fields and a checkbox which is checked by default and it means the shipping address and the billing address are same. If it is unchecked then I have to provide a billing address. I have a button which will show the payment option if the fields are filled up with correct data.
The validation is working fine as long as the checkbox is checked. But it is failing to validate the billing address fields if the checkbox is unchecked.
What I have done:
I have disabled and hid the billing address fields if the checkbox is checked. Once unchecked, I am showing the billing address fields with all the fields in enabled and required state. The reason behind this is if I don't disable the fields then the hidden billing address fields are considered in the field validation.
The view page with the Angular validation:
<form class="form-horizontal address" name="addressForm" novalidate>
<div class="panel-body">
<div class="form-group">
<div class="col-md-12">
<h4>Shipping Address</h4>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.country.$invalid && !addressForm.country.$pristine }">
<div class="col-md-12"><strong>Country:</strong></div>
<div class="col-md-12">
<input type="text" name="country" class="form-control" value="" ng-model="shipCountry" ng-required="true" />
<p ng-show="addressForm.country.$invalid && !addressForm.country.$pristine" class="help-block">Country name is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.fname.$invalid && !addressForm.fname.$pristine }">
<div class="col-md-12"><strong>Full Name:</strong></div>
<div class="col-xs-12">
<input type="text" name="fname" class="form-control" value="" ng-model="shipFullName" ng-required="true" />
<p ng-show="addressForm.fname.$invalid && !addressForm.fname.$pristine" class="help-block">Your name is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.address.$invalid && !addressForm.address.$pristine }">
<div class="col-md-12"><strong>Address:</strong></div>
<div class="col-md-12">
<input type="text" name="address" class="form-control" value="" ng-model="shipAddress" ng-required="true" />
<p ng-show="addressForm.address.$invalid && !addressForm.address.$pristine" class="help-block">Your address is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.city.$invalid && !addressForm.city.$pristine }">
<div class="col-md-12"><strong>City:</strong></div>
<div class="col-md-12">
<input type="text" name="city" class="form-control" value="" ng-model="shipCity" ng-required="true" />
<p ng-show="addressForm.city.$invalid && !addressForm.city.$pristine" class="help-block">Your city is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.state.$invalid && !addressForm.state.$pristine }">
<div class="col-md-12"><strong>State:</strong></div>
<div class="col-md-12">
<input type="text" name="state" class="form-control" value="" ng-model="shipState" ng-required="true" />
<p ng-show="addressForm.state.$invalid && !addressForm.state.$pristine" class="help-block">Your state is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.zip_code.$invalid && !addressForm.zip_code.$pristine }">
<div class="col-md-12"><strong>Zip / Postal Code:</strong></div>
<div class="col-md-12">
<input type="text" name="zip_code" class="form-control" value="" ng-model="shipPostal" ng-required="true" ng-pattern="/^[0-9]+$/" maxlength="7" />
<p ng-show="addressForm.zip_code.$invalid && !addressForm.zip_code.$pristine" class="help-block">Error in postal code field!!</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.phone_number.$invalid && !addressForm.phone_number.$pristine }">
<div class="col-md-12"><strong>Phone Number:</strong></div>
<div class="col-md-12">
<input type="text" name="phone_number" class="form-control" value="" ng-model="shipPhone" ng-required="true" maxlength="10"
ng-pattern="/^[0-9]+$/" />
<p ng-show="addressForm.phone_number.$invalid && !addressForm.phone_number.$pristine" class="help-block">Error in phone number field!!</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.email_address.$invalid && !addressForm.email_address.$pristine }">
<div class="col-md-12"><strong>Email Address:</strong></div>
<div class="col-md-12">
<input type="text" name="email_address" class="form-control" value="" ng-model="shipEmail" ng-required="true" ng-pattern="/^(([^<>()\[\]\\.,;:\s#']+(\.[^<>()\[\]\\.,;:\s#']+)*)|('.+'))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/" />
<p ng-show="addressForm.email_address.$invalid && !addressForm.email_address.$pristine" class="help-block">Error in email field!!</p>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="checkbox" name="address" class="" value="" /> Save address
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="checkbox" name="address" class="" value="" ng-model="billSameAsShip" ng-change="toggleBillAddressView()" />Billing Address same as Shipping Address
</div>
</div>
</div>
<!--SHIPPING METHOD END-->
<!--BILLING METHOD START-->
<div class="panel-body" ng-hide="billSameAsShip">
<div class="form-group">
<div class="col-md-12">
<h4>Billing Address</h4>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.bill_country.$invalid && !addressForm.bill_country.$pristine }">
<div class="col-md-12"><strong>Country:</strong></div>
<div class="col-md-12">
<input type="text" class="form-control" name="bill_country" value="" ng-model="billCountry" ng-disabled="billSameAsShip" ng-required="{{!billSameAsShip}}" />
<p ng-show="addressForm.bill_country.$invalid && !addressForm.bill_country.$pristine" class="help-block">Your billing country is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.bill_fname.$invalid && !addressForm.bill_fname.$pristine }">
<div class="col-xs-12">
<strong>Full Name:</strong>
<input type="text" name="bill_fname" class="form-control" value="" ng-model="billFullName" ng-disabled="billSameAsShip" ng-required="{{!billSameAsShip}}" />
<p ng-show="addressForm.bill_fname.$invalid && !addressForm.bill_fname.$pristine" class="help-block">Your full name is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.bill_address.$invalid && !addressForm.bill_address.$pristine }">
<div class="col-md-12"><strong>Address:</strong></div>
<div class="col-md-12">
<input type="text" name="bill_address" class="form-control" value="" ng-model="billAddress" ng-disabled="billSameAsShip" ng-required="{{!billSameAsShip}}" />
<p ng-show="addressForm.bill_address.$invalid && !addressForm.bill_address.$pristine" class="help-block">Your billing address is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.bill_city.$invalid && !addressForm.bill_city.$pristine }">
<div class="col-md-12"><strong>City:</strong></div>
<div class="col-md-12">
<input type="text" name="bill_city" class="form-control" value="" ng-model="billCity" ng-disabled="billSameAsShip" ng-required="{{!billSameAsShip}}" />
<p ng-show="addressForm.bill_city.$invalid && !addressForm.bill_city.$pristine" class="help-block">Your billing city is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.bill_state.$invalid && !addressForm.bill_state.$pristine }">
<div class="col-md-12"><strong>State:</strong></div>
<div class="col-md-12">
<input type="text" name="bill_state" class="form-control" value="" ng-model="billState" ng-disabled="billSameAsShip" ng-required="{{!billSameAsShip}}" />
<p ng-show="addressForm.bill_state.$invalid && !addressForm.bill_state.$pristine" class="help-block">Your billing state is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.bill_zip_code.$invalid && !addressForm.bill_zip_code.$pristine }">
<div class="col-md-12"><strong>Zip / Postal Code:</strong></div>
<div class="col-md-12">
<input type="text" name="bill_zip_code" class="form-control" value="" ng-model="billPostal" ng-disabled="billSameAsShip" ng-required="{{!billSameAsShip}}" />
<p ng-show="addressForm.bill_zip_code.$invalid && !addressForm.bill_zip_code.$pristine" class="help-block">Your billing zip/postal code is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.bill_phone_number.$invalid && !addressForm.bill_phone_number.$pristine }">
<div class="col-md-12"><strong>Phone Number:</strong></div>
<div class="col-md-12">
<input type="text" name="bill_phone_number" class="form-control" value="" ng-model="billPhone" ng-disabled="billSameAsShip" ng-required="{{!billSameAsShip}}" />
<p ng-show="addressForm.bill_phone_number.$invalid && !addressForm.bill_phone_number.$pristine" class="help-block">Your billing phone number is required.</p>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error' : addressForm.bill_email_address.$invalid && !addressForm.bill_email_address.$pristine }">
<div class="col-md-12"><strong>Email Address:</strong></div>
<div class="col-md-12">
<input type="text" name="bill_email_address" class="form-control" value="" ng-model="billEmail" ng-disabled="billSameAsShip" ng-required="{{!billSameAsShip}}" />
<p ng-show="addressForm.bill_email_address.$invalid && !addressForm.bill_email_address.$pristine" class="help-block">Your billing email address is required.</p>
</div>
</div>
</div>
<div class="panel-footer">
<div class="form-group">
<div class="col-xs-12 text-right">
<button type="button" class="btn btn-primary btn-submit-fix" ng-click="gotoInvoice()">Show Payment Option</button>
</div>
</div>
</div>
</form>
Don't use ng-hide. Use ng-if. ng-if will remove the fields from the form, which thus won't take them into accound when validating the form.

How to validate forms without using disable button

I am trying to validat my form and i done it but the problem is when i click the button without entering any input value it is getting saved and i dont want to disable the button.I need a condition that if i click on submit it should show the fields error that are empty.Can anyone please suggest help.
<div class="loginformcss nopadding">
<form id="login-form" name="login-form" class="nobottommargin" [formGroup]="form" (ngSubmit)="onSubmit(form.value)">
<div class="col_full">
<input type="text" [formControl]="form.controls['firstname']" id="login-form-firstnamee" name="login-form-firstname" value="" placeholder="First Name" class="form-control not-dark">
<span *ngIf="form.controls['firstname'].touched">
<span *ngIf="!form.controls['firstname'].valid" >
<p class="error">Field required</p>
</span>
</span>
</div>
<div class="clear"></div>
<div class="col_full">
<input type="text" [formControl]="form.controls['lastname']" id="login-form-password" name="login-form-password" value="" placeholder="Last Name" class="form-control not-dark">
<span *ngIf="form.controls['lastname'].touched" >
<span *ngIf="!form.controls['lastname'].valid" >
<p class="error">Field required</p>
</span>
</span>
</div>
<div class="clear"></div>
<div class="col_full">
<input type="text" [formControl]="form.controls['profilename']" id="login-form-username" name="login-form-username" value="" placeholder="User Name" class="form-control not-dark">
<span *ngIf="form.controls['profilename'].touched" >
<span *ngIf="!form.controls['profilename'].valid" >
<p class="error">Field required</p>
</span>
</span>
</div>
<div class="clear"></div>
<div class="col_full nobottommargin">
<button class="col-xs-12 button button-small button-3d nomargin" id="login-form-submit" name="login-form-submit" value="login" >Login</button>
</div>
<div class="clear"></div>
The easiest way is to add required attribute to your input

how to display all error messages in the form header in angularjs

I need to display all the messages at the top of the form and I need to implement the custom error for fields like email and phone number.
index.html
<form name="adminForm" ng-submit="addAdmin()" novalidate>
all error messages need to be displayed here
<div class="form-group" ng-class="{ 'has-error': adminForm.firstName.$touched && adminForm.firstName.$invalid }">
<label>First Name</label>
<input type="text" name="firstName" class="form-control" ng-model="admin.first_name" ng-maxlength="255" required>
<div class="help-block" ng-messages="adminForm.firstName.$error" ng-if="adminForm.firstName.$touched">
<div ng-messages-include="messages.html"></div>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': adminForm.lastName.$touched && adminForm.lastName.$invalid }">
<label>Last Name</label>
<input type="text" name="lastName" class="form-control" ng-model="admin.last_name" ng-maxlength="255" required>
<div class="help-block" ng-messages="adminForm.lastName.$error" ng-if="adminForm.lastName.$touched">
<div ng-messages-include="messages.html"></div>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': adminForm.email.$touched && adminForm.email.$invalid }">
<label>Email</label>
<input type="email" name="email" class="form-control" ng-model="admin.email" ng-maxlength="255" required>
<div class="help-block" ng-messages="adminForm.email.$error" ng-if="adminForm.email.$touched">
<div ng-messages-include="messages.html"></div>
</div>
</div>
messages.html
<p ng-message="required">This field is required</p>
<p ng-message="minlength">This field is too short</p>
<p ng-message="maxlength">This field is too long</p>
<p ng-message="email">This needs to be a valid email</p>
<p ng-message="pattern">This needs to be a valid number</p>
<p ng-message="equals">Password must be same</p>
I'm looking for any possible solution or a reference to other possible solutions.
Reason : I'm finding difficulty in implementing unit tests for this(noob), so i think a single error message can make it easy for testing.
Thanks!

Array of elements is not working correctly

I have the following mongoose schema
var MySchema = new Schema({
field1: [{
format: {
type: String,
required: true
},
value: {
type: String,
required: true
}
}],
field2: [{
format: {
type: String,
required: true
},
value: {
type: String,
required: true
}
}]
});
To get the input for this model I am creating a form which has the array of controls and the code is below
Even If if try to use the $scope.field1[<index>] it give the error that
TypeError: Cannot read property 'field1' of undefined
<div class="form-group">
<div class="col-md-12 padding-left-0">
<label for="question">Field1 Values</label>
</div>
<div class="col-md-12" ng-repeat="(optionKey, optionValue) in [0,1]">
<ng-form name="field1Form{{optionKey}}">
<div class="col-md-2" ng-class="{ 'has-error' : submitted && field1Form{{optionKey}}.field1_format.$invalid }">
<label for="field1_format">Type</label>
<select class="form-control" name="field1_format" id="field1_format" data-ng-model="form.field1[$index].format" required>
<option value="text" selected="selected">Text</option>
<option value="image">Image</option>
</select>
<div ng-show="submitted && field1Form{{optionKey}}.field1_format.$invalid" class="help-block">
<p ng-show="field1Form{{optionKey}}.field1_format.$error.required">Field1 type is required</p>
</div>
</div>
<div class="col-md-10" ng-class="{ 'has-error' : submitted && field1Form{{optionKey}}.field1_value.$invalid }">
<div class="col-md-12">
<label for="field1_value">Value</label>
</div>
<div class="col-md-12">
<input type="text" class="form-control" name="field1_value" id="field1_value" data-ng-model="form.field1[$index].value" placeholder="Enter field1 value" required/>
<div ng-show="submitted && field1Form{{optionKey}}.field1_value.$invalid" class="help-block">
<p ng-show="field1Form{{optionKey}}.field1_value.$error.required">Field1 value is required</p>
</div>
</div>
</div>
</ng-form>
</div>
</div>
<div class="form-group col-md-12 padding-left-0" data-ng-show="true">
<div class="col-md-12 padding-left-0"><label>Field2 Values</label></div>
<div class="col-md-6" data-ng-repeat="i in [0,1]">
<ng-form name="field2Form{{i}}">
<label>Value-{{i+1}}</label>
<div class="col-md-12">
<div class="col-md-4" ng-class="{ 'has-error' : submitted && field2Form{{i}}.field2_format.$invalid }">
<label for="field2_format">Type</label>
<select class="form-control" name="field2_format" id="field2_format" data-ng-model="form.field2[$index].format" required>
<option value="text">Text</option>
<option value="image">Image</option>
</select>
<div ng-show="submitted && field2Form{{i}}.field2_format.$invalid" class="help-block">
<p ng-show="field2Form{{i}}.field2_format.$error.required">Field2 type is required</p>
</div>
</div>
<div class="col-md-7" ng-class="{ 'has-error' : submitted && field2Form{{i}}.field2_value.$invalid }">
<label for="field2_value">Value</label>
<input type="text" class="form-control" name="field2_value" id="field2_value" data-ng-model="form.field2[$index].value" placeholder="Enter Value {{i+1}}" required/>
<div ng-show="submitted && field2Form{{i}}.field2_value.$invalid" class="help-block">
<p ng-show="field2Form{{i}}.field2_value.$error.required">Field2 value is required</p>
</div>
</div>
</div>
</ng-form>
</div>
</div>
But in the angular controller I am getting the array of object values for field2 but field1 is not coming (missing) from the form data.
But when I hard code the question array like below for field1 then field1 also work fine
<div class="form-group">
<div class="col-md-12">
<ng-form name="field1Form0">
<div class="col-md-2" ng-class="{ 'has-error' : submitted && field1Form0.field1_format.$invalid }">
<label for="field1_format">Type</label>
<select class="form-control" name="field1_format" id="field1_format" data-ng-model="form.field1[0].format" required>
<option value="text">Text</option>
<option value="image">Image</option>
</select>
<div ng-show="submitted && field1Form0.field1_format.$invalid" class="help-block">
<p ng-show="field1Form0.field1_format.$error.required">Field1 type is required</p>
</div>
</div>
<div class="col-md-10" ng-class="{ 'has-error' : submitted && field1Form0.field1_value.$invalid }">
<div class="col-md-12">
<label for="field1_value">Value</label>
</div>
<div class="col-md-12">
<input type="text" class="form-control" name="field1_value" id="field1_value" data-ng-model="form.field1[0].value" placeholder="Enter field1" required/>
<div ng-show="submitted && field1Form0.field1_value.$invalid" class="help-block">
<p ng-show="field1Form0.field1_value.$error.required">Question value is required</p>
</div>
</div>
</div>
</ng-form>
</div>
</div>
Can anyone help me in this?
It seems like some angular issue.
It has been fixed by declaring the variable in angular Controller like below
$scope.form = {};
$scope.form.field1 = {};
Now its working fine. May be the issue in auto declaring the nested objects or array in angular.
It very possible you've got a lot of things mixed up.
One of them you have already recognised - the scope didn't have a form property.
The expression field1Form{{optionKey}}.field1_format.$invalid is not valid, because string interpolation cannot be used in the context where it's found. As far as I know, ng-class and ng-show directives expect a JavaScript expression, not a template. My point is this clearly, the interpolation expression {{optionKey}}, should not be used in ng-class and ng-show directives.

Angular.js Form Invalid- Don't know why

I don't know why, but by default in Angular.js this for is invalid by dafault(no interaction on the form):
<!-- Persistence -->
<div class="col-sm-6" ng-class="{'has-error': form.persistence.$dirty && form.persistence.$invalid}">
<div class="checkbox">
<input for="persistence" type="checkbox" ng-change="resetPersistenceInputBox(form, vip)" ng-model="persistanceCheckbox" ng-init="persistanceCheckbox=false">Client IP Based Persistence
<div>
<label class="control-label" style="font-weight: bold">Enter CIDR Netmask</label>
<div class="input-group">
<span class="input-group-addon">
x.x.x.x/
</span>
<input type="text" class="col-sm-6 form-control" id="persistence" ng-init="persistanceCheckbox=false" name="persistence"
ng-disabled="!persistanceCheckbox"
ng-required="persistanceCheckbox"
ng-model="vip.algorithm.persistence"
ng-pattern='/^([0-9]|[12]\d|3[0-2])$/'
ng-trim="true"
placeholder="Enter a netmask between 0-32">
</div>
<div class="help-block" ng-show="form.persistence.$dirty && form.persistence.$invalid">
<span ng-show="form.persistence.$error.pattern && form.persistence.$dirty">Netmask must be a number between 0-32.</span>
<span ng-show="form.persistence.$error.required">CIDR Netmask is required while "Client IP Based Persistence" is checked.</span>
</div>
</div>
</div>
</div>
Your form is not valid until you type the input
<input type="text" class="col-sm-6 form-control" id="persistence" ng-init="persistanceCheckbox=false" name="persistence"
ng-disabled="!persistanceCheckbox"
ng-required="persistanceCheckbox"
ng-model="vip.algorithm.persistence"
ng-pattern='/^([0-9]|[12]\d|3[0-2])$/'
ng-trim="true"
placeholder="Enter a netmask between 0-32">
The input is required with ng-required="persistanceCheckbox"
and must meet the ng-pattern : ng-pattern='/^([0-9]|[12]\d|3[0-2])$/'

Resources