Not able to add validations to the elements in my form - angularjs

I want to add validations to all the elements in the form. the error message should be displayed below the elements when the text of the control changes.
Following is my code:
<div role="tabpanel" class="tab-pane active" id="step-1">
<div class="col-md-4">
<div> <img src="../logo.jpg"
alt="Smiley face"
height="150"
style="margin:40px;"
width="150">
</div>
</div>
<div class="col-md-7">
<form action="r" name="regform"
method="post" >
<div class="col-md-6">
<div class="col-md-6" style="padding:0px;" >
<label for="mType" >Member Type*</label> <br />
<select id="member" ng-model="inputForm.mType" style="height:35px;width:135px;">
<option value="owner">Owner</option>
<option value="agent">Agent</option>
<option value="agent">Customer</option>
</select>
</div>
<div class="form-group">
<div class="col-md-6" style="padding:0px;" >
<label for="gender" >Gender </label> <br />
<select id="gender" ng-model="inputForm.gender" style="height:35px;width:135px;">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
</div>
</div>
<div class="form-group">
<label for=''>First Name</label>
<input type='name'
name='fName'
ng-model="inputForm.fName"
ng-minlength="1"
ng-maxlength="25"
ng-pattern="/^[A-Za-z]+$/"
required
/>
<span ng-show="regform.fName.$error.pattern">Please enter valid number!</span>
<!-- <span ng-show="studentForm.firstName.$touched && studentForm.firstName.$error.required">First name is required.</span>
<span ng-show="studentForm.lastName.$touched && studentForm.lastName.$error.minlength">min 3 chars.</span>
<span ng-show="studentForm.lastName.$touched && studentForm.lastName.$error.maxlength">Max 10 chars.</span>-->
</div>
<div class="form-group">
<label>Last Name : </label>
<input type="text"
name="lName"
class="form-control input-lg"
placeholder="Last Name"
ng-model="inputForm.lName"
style="height:35px">
</input>
</div>
<div class="form-group">
<label for="DOB">Date of Birth :</label>
<input type="date"
id="dob"
class="form-control input-lg"
placeholder="Date Of Birth (mm/dd/yyyy)"
ng-model="inputForm.dob"
style="height:35px;">
</input>
</div>
<div class="form-group">
<label>Adhar Number:</label>
<input type="text" id="adhar"
class="form-control input-lg"
placeholder="Adhar Number"
ng-model="inputForm.adhar"
style="height:35px">
</input>
</div>
<div class="form-group">
<label>PAN Number :</label>
<input type="text" id="pan"
class="form-control input-lg"
placeholder="PAN Number"
ng-model="inputForm.pan"
style="height:35px">
</input>
</div>
<div class="form-group">
<label>Email Address :</label>
<input type="email" id="email"
class="form-control input-lg"
placeholder="Your Email"
ng-model="inputForm.email"
style="height:35px">
</input>
<!-- <span ng-show="studentForm.email.$touched && studentForm.email.$error.email">Please enter valid email id.</span>-->
</div>
</div>
<div class="col-md-5" >
<div class="full-width bg-transparent">
<div class="full-width">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="custom-form">
<div class="text-center bg-form">
<div class="img-section">
<img src="http://lorempixel.com/200/200/nature/"
class="imgCircle"
alt="Profile picture">
<span class="fake-icon-edit"
id="PicUpload"
style="color: #ffffff;">
<span class="glyphicon glyphicon-camera camera"></span>
</span>
<div class="col-lg-12">
<h4 class="text-right col-lg-12" style="color:balck;">
<span class="glyphicon glyphicon-edit"></span> Edit Profile
</h4>
<input type="checkbox" class="form-control" id="checker"></input>
</div>
</div>
<input type="file"
id="image-input"
onchange="readURL(this);"
accept="image/*"
disabled class="form-control form-input Profile-input-file" >
</input>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-8" >
<hr class="colorgraph" style="height: 5px;
border-top: 0;
background: #62c2e4;
border-radius: 5px;">
<div class="col-xs-4 col-xs-offset-3">
<a ui-sref="form.account" class="btn btn-lg btn-primary btn-block signup-btn"
style=" height:35px;
margin-bottom:10px;
padding:0px;" >
Next <span class="glyphicon glyphicon-circle-arrow-right"></span>
</a>
</div>
</div>
</form>
</div>
</div>
image for reference
Please give suggestions to change the code so that appropriate error messages will be displayed to the controls after validating the input entr the user.

Your question is a bit vague as we can do what you are asking in many ways but find below an example using Bootstrap :
<form name="search" class="form-horizontal">
<div class="form-group col-md-2 required" ng-class="{
'has-feedback': (search.$submitted && search.carrier.$invalid),
'has-error': (search.$submitted && search.carrier.$invalid)}">
<label class="control-label" for="carrier">Carrier Code:</label>
<input type="text" class="form-control"
id="carrier" name="carrier"
required
pattern="[a-zA-Z0-9-]{2,3}"
maxlength="3"
ng-model="myAngularCtrl.flightDetails.carrier"/>
<span ng-show="search.$submitted && search.carrier.$error.required" class="field_errormsg-below">The Carrier Code is Mandatory</span>
<span ng-show="search.$submitted && search.carrier.$error.pattern" class="field_errormsg-below">use only numbers, digit and -</span>
</div>
<button type="submit"
class="btn btn-default FHU_margin-top-20"
ng-click="myAngularCtrl.searchFlight(search)">
Submit
</button>
</form>
You have a few things here:
The form has a name: search
You see two kind of error message being displayed. For them to be displayed, the user needs to have tried to submit the form (search.$submitted) and the field carrier needs to be in error (search.carrier.$error.required or search.carrier.$error.pattern)

Related

ng-submit is not working in AngularJs while controller is properly working

I am relatively new to AngularJs. I am creating a website and there are several forms on the website. The same code with ng-submit is working on other pages. But not here.
The controller, ng-options and validations are working properly.
I tried changing function and checking the function
<div class="row" ng-controller="PostspaceFormController as LoginFormCtrl">
<div class="divider-30 d-none d-lg-block"></div>
<form class="form-styling" name="LoginForm" ng-submit="submitDataInForm3()" novalidate>
<fieldset ng-disabled="sendingMail">
<p class="fs-l-17 fw-l-500 margin-styling">Post Your Space Requirement</p>
<div class="form-group m-0" ng-class="{'has-error': LoginForm.email.$invalid && LoginForm.email.$touched}">
<label class="form-label label-styling">Your Email</label>
<input type="email" placeholder="" class="form-control input-styling" ng-model="data.email" name="email" required pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,4}$">
<div class="error-state ">
<span class="error-label text-danger margin-styling" ng-show="LoginForm.email.$error.required && LoginForm.email.$touched">Email is Required</span>
<span class="error-label text-danger margin-styling" ng-show="LoginForm.email.$touched && LoginForm.email.$invalid && !LoginForm.email.$error.required">Invalid Email Address</span>
</div>
</div>
<div class="form-group m-0" ng-class="{'has-error': LoginForm.mobile.$invalid && LoginForm.mobile.$touched}">
<label class="form-label label-styling">Your Mobile</label>
<input type="text" placeholder="" class="form-control input-styling" ng-model="data.mobile" name="mobile" required pattern="^\d{4,12}$">
<div class="error-state ">
<span class="error-label text-danger margin-styling" ng-show="LoginForm.mobile.$error.required && LoginForm.mobile.$touched">Mobile Number is Required</span>
<span class="error-label text-danger margin-styling" ng-show="LoginForm.mobile.$touched && LoginForm.mobile.$invalid && !LoginForm.mobile.$error.required">Invalid Mobile Number</span>
</div>
</div>
<div class="form-group m-0" ng-class="{'has-error': LoginForm.name.$invalid && LoginForm.name.$touched}">
<label class="form-label label-styling">Your Name</label>
<input type="text" class="form-control input-styling" ng-model="data.name" name="name" placeholder="" required>
<div class="error-state ">
<span class="error-label text-danger margin-styling" ng-show="LoginForm.name.$error.required && LoginForm.name.$touched">Name is Required</span>
<!-- <span class="error-label text-danger margin-styling" ng-show="LoginForm.name.$touched && LoginForm.name.$invalid && !LoginForm.name.$error.required">Invalid Name</span> -->
</div>
</div>
<div class="form-group m-0">
<label class="form-label label-styling">What are you looking for?</label>
<select class="form-control input-styling" required name="spacetype" ng-change="changeSelected(workingSpace)" ng-model="workingSpace" ng-options="type as type.category for type in spaceTypes" ng-class="{'not-selected': !workingSpace}">
<option value="" disabled="">Select Type Of Space</option>
</select>
</div>
<div class="justify-content-center d-flex">
<button type="submit" class="btn-color fs-l-14 h5 btn-block btn-lg text-center rounded-0 margin-styling border-0" ng-disabled="sendingMail" ng-class="{'running': sendingMail}">
<span ng-hide="sendingMail">BOOK YOUR SPACE NOW</span>
<span ng-show="sendingMail">Submitting</span>
<div ng-show="sendingMail" class="ld ld-ring ld-spin"></div>
</button>
</div>
</fieldset>
</form>
</div>

Can not access ng-model values inside controller [angularJS]

When I try to access user.firstname value in controller, it gives the following error.
TypeError: Cannot read property 'firstname' of undefined
$scope.signUpCustomer = function(){
console.log("GGGGGGGGGGGGGGGGGGGGGGGGG ", $scope.user.firstname);
}
<form class="form-horizontal font-hp-simplified signUpUserForm row" role="form" ng-submit="signUpCustomer()" name ="signUpUserForm" ng-show="!userVendor">
<div class="col-md-12">
<div class="col-md-6">
<div class="form-group">
<label for="firstname" class="col-md-12">First Name:</label>
<div class="col-md-12">
<input ng-model="user.firstname" type="text" class="form-control" name="firstname" required />
<span class="text-danger" ng-show="signUpUserForm.firstname.$invalid && signUpUserForm.firstname.$dirty ">First name is required</span>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="lastname" class="col-md-12">Last Name:</label>
<div class="col-md-12">
<input ng-model="user.lastname" type="text" class="form-control" name="lastname" required />
<span class="text-danger" ng-show="signUpUserForm.lastname.$invalid && signUpUserForm.lastname.$dirty ">Last name is required</span>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="col-md-6">
<div class="form-group">
<label for="email" class="col-md-12">Email:</label>
<div class="col-md-12">
<input ng-model="user.email" type="email" class="form-control" name="email" required />
<span class="text-danger" ng-show="signUpUserForm.email.$error.required && signUpUserForm.email.$dirty ">Email is required</span>
<span class="text-danger" ng-show="signUpUserForm.email.$error.email && signUpUserForm.email.$dirty ">Please enter valid email</span>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="password" class="col-md-12">Password:</label>
<div class="col-md-12">
<input ng-model="user.password" type="password" class="form-control" name="password" required />
<span class="text-danger" ng-show="signUpUserForm.password.$invalid && signUpUserForm.password.$dirty">Password is required</span>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="col-md-6">
<div class="form-group">
<label for="phone" class="col-md-12">Phone:</label>
<div class="col-md-12">
<input ng-model="user.phone" type="text" class="form-control" name="phone" required />
<span class="text-danger" ng-show="signUpUserForm.phone.$invalid && signUpUserForm.phone.$dirty">Phone is required</span>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="country" class="col-md-12">Country:</label>
<div class="col-md-12">
<select name="country" ng-model="user.country" type="text" class="form-control form-bg-white" required>
<!-- <option value="" disabled selected>Select Your Country</option> -->
<option value="" disabled selected></option>
<option ng-repeat="country in countries" value="{{country}}">{{country}}</option>
</select>
<span class="text-danger" ng-show="signUpUserForm.country.$invalid && signUpUserForm.country.$dirty">Country is required</span>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="col-md-12">
<!-- Button -->
<div class="form-group">
<div class="col-md-12">
<div class="icon-user-signup">
<div class="input-group-addon">
<i class="fa fa-user"></i>
</div>
<button id="btn-signup" class="btn btn-primary col-md-12">SIGN UP</button>
</div>
</div>
<!-- <div class="mrg-top-10 col-md-12 text-right">
<h4><a ng-click="go('/seller#/signup')" class="ven-link">
Become a vendor today
</a></h4>
</div>-->
</div>
</div>
</div>
<!-- <div class="col-md-12 control">
<div class="signup-border" >
Already have an account ?
<a ui-sref="loginUser" >
Sign In Here
</a>
</div>
</div> -->
</form>
You need to initialize the $scope.customer first in controller
$scope.user = {};

Ng-click validation is not working

I am beginer in angular js. I am validating a form with some input feild and form is posting on ng-click but validation is not working, validation message are displaying for a white then disappear i have to submit the form after validating. form ng-click should not be called untill the form is valid please help me . Thanks in advance.
<form name="teamForm" novalidate ng-submit="submit(teamForm)" class="formfields">
<div class="col-md-12">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="lname">First Name:</label>
<input type="text" name="firstname"
ng-model="FirstName" class="form-control custom-form-control"
placeholder="First Name" required="required">
<span class="text-danger"
ng-show="(teamForm.firstname.$dirty || submitted) && teamForm.firstname.$error.required">Required</span>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="lname">Last Name:</label>
<input type="text" name="lastname"
ng-model="LastName" class="form-control custom-form-control"
placeholder="Last Name" required="required">
<span class="text-danger"
ng-show="(teamForm.lastname.$dirty || submitted) && teamForm.lastname.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="email">Email:</label>
<input type="text" name="email"
ng-model="Email" class="form-control custom-form-control"
ng-pattern="/^[^\s#]+#[^\s#]+\.[^\s#]{2,}$/"
placeholder="Email" required="required">
<span class="text-danger"
ng-show="(teamForm.email.$dirty || submitted) && teamForm.email.$error.required">Required</span>
<span class="text-danger"
ng-show="teamForm.email.$dirty &&teamForm.email.$error.pattern">Please Enter Valid Email</span>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label>Phone Number:</label>
<div class="clearfix"></div>
<input type="text" name="phone"
ng-model="Phone" class="form-control custom-form-control"
placeholder="XXXXXXXXXX" required="required">
<span class="text-danger"
ng-show="(teamForm.phone.$dirty || submitted) && teamForm.phone.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<label>Message:</label>
<textarea class="form-control rounded-0" rows="5"
name="comment" placeholder="Message"
ng-model="Comment" required="required"></textarea>
<span class="text-danger"
ng-show="(teamForm.comment.$dirty || submitted) && teamForm.comment.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<label>Upload Resume:</label>
<!--<input type="file" name="ResumePath" id="filehandler" />-->
<input type="file" id="file1" name="file" class="filelabel sr-only" multiple ng-files="getTheFiles($files)" onchange="Checkfiles($(this))" />
<!-- <input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/> -->
<label for="file1" class="form-control">
<span><i class="fa fa-file"></i> Drag file here or choose file</span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div vc-recaptcha key="'6Lc860IUAAAAAAyWI9WD8EV4eITu4ODdhuYHdzi8'"
class="grecaptcha" ng-model="respone1"></div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<button type="button" id="btnSubmit"
ng-click="uploadFiles()" value="Upload"
class="btn btn-green center-block pull-left">
<i class="fa fa-send"></i>{{btnText}}</button>
</div>
</div>
<div class="form-group text-center">
<h5 class="text-success" style="font-weight:bold">{{messagesuccess}}</h5>
<h5 class="text-danger" style="font-weight:bold">{{messageerror}}</h5>
</div>
</div>
</form>
ng-click (or it's vanilla cousin, onclick) do not check form validation. The function for submission needs to be defined at the form level, and then you specify which button acts as the submit button in order to get form behavior.
I see you already have a submit function defined. I assume you want to change that to uploadFiles. And if you want the form to conduct validation, remove the novalidation attribute.
<form name="teamForm" ng-submit="uploadFiles()" class="formfields">
then, for the button you would specify it is the submission button and remove the ng-click.
<button type="submit" id="btnSubmit"
value="Upload"
class="btn btn-green center-block pull-left">
<i class="fa fa-send"></i>{{btnText}}</button>

Angular js $form.valid is not working

I am working with Angular 1.6.4 version in my project.
I am using the following template for create user details.Here I am making some validations for the fields, until it fulfills the validation, save button will not be enabled.
This is working fine, and error messages for validation works fine in creating users.
I am using the same page to update users, while the user details will be populated in textfields, Here the save button is not enabled even though user details have been populated.
Can anyone assist here in this.
Here is my code,
<form name="userForm">
<div class="header-buttons3">
<button type="button" class="header-button-save btn" ng-disabled="userForm.$invalid" ng-click="submitUserForm()"><i class="fa fa-pencil"></i>SAVE</button>
<button type="button" class="header-button-cancel"><i class="fa fa-chevron-left"></i>CANCEL</button>
</div>
<div class="clearfix"></div>
<div class="row">
<div class="col-md-11 col-sm-12 col-xs-12">
<div class="x_content">
<br>
<div class="form-row row">
<div class="form-group required col-md-3">
<label for="first name" class="control-label">FIRST NAME</label>
<span class="after">*</span> <input class="form-control"
ng-class="{validbr: showfn==='false', invalidbr: showfn }"
ng-focus="showfn=false"
ng-blur="showfn=userForm.firstName.$invalid" id="firstname"
class="form-control" ng-model="user.firstName"
ng-pattern="/^[a-zA-Z]*$/" name="firstName" ng-maxlength="30"
maxlength="30" required placeholder="Enter First Name" />
<div style="color: Red"
ng-show="showfn && userForm.firstName.$error.required">Error
- First Name Required</div>
<div style="color: Red"
ng-show="showfn && userForm.firstName.$error.pattern">Error
- Enter only Character</div>
<div style="color: Red"
ng-show="showfn && userForm.firstName.$error.maxlength">Error
- Need 30 char max</div>
</div>
<div class="form-group required col-md-3">
<label for="last name" class="control-label">LAST NAME</label> <span
class="after">*</span> <input class="form-control"
ng-class="{validbr: showln==='false', invalidbr: showln }"
ng-focus="showln=false"
ng-blur="showln=userForm.lastName.$invalid" id="lastname"
ng-model="user.lastName" ng-pattern="/^[a-zA-Z]*$/"
name="lastName" ng-maxlength="50" maxlength="50" required
placeholder="Enter Last Name" />
<div style="color: Red"
ng-show="showln && userForm.lastName.$error.required">Error
- Last Name Required</div>
<div style="color: Red"
ng-show="showln && userForm.lastName.$error.pattern">Error
- Enter only Character</div>
<div style="color: Red"
ng-show="showln && userForm.lastName.$error.maxlength">Error
- Need 50 char max</div>
</div>
<div class="form-group required col-md-3">
<label for="inputEmail4" class="control-label">EMAIL
ADDRESS</label> <span class="after">*</span>
<input type="email" readonly class="form-control"
ng-class="{validbr: showem==='false', invalidbr: showem }"
ng-focus="showem=false"
ng-blur="showem=userForm.emailId.$invalid" id="emailId"
ng-model="user.emailId" name="emailId" ng-maxlength="70"
maxlength="70" required placeholder="Enter Email Address" />
<div style="color: Red"
ng-show="showem && userForm.emailId.$error.required">Error
- Email address Required</div>
<div style="color: Red"
ng-show="showem && userForm.emailId.$error.email">Error -
Not a valid email address</div>
</div>
<div class="form-group required col-md-3">
<label for="inputEmail4" class="control-label">USER STATUS
</label><br/>
<label>
<input type="radio" ng-model="user.active" ng-value="false">
IN ACTIVE
</label>
<label>
<input type="radio" ng-model="user.active" ng-value="true">
ACTIVE
</label>
</div>
</div>
<div class="form-row row">
<div class="form-group col-md-3">
<label for="mobile phone" class="col-form-label">MOBILE
PHONE</label> <input class="form-control" id="mobileNumber"
ng-class="{validbr: showmn==='false', invalidbr: showmn }"
ng-focus="showmn=false"
ng-blur="showmn=userForm.mobileNumber.$invalid"
ng-model="user.mobileNumber" name="mobileNumber"
ng-minlength="14" ng-maxlength="14" phone-input
placeholder="Enter Mobile Phone #" />
<div style="color: Red"
ng-show="showmn && userForm.mobileNumber.$error.minlength ">Error
- Need 10 digits for a Phone #</div>
<div style="color: Red"
ng-show="showmn && userForm.mobileNumber.$error.maxlength ">Error
- Need 10 digits for a Phone #</div>
<!-- <div ng-show="userForm.mobileNumber.$error.minlength">Error - Need 10 digits for a Phone #</div> -->
</div>
<div class="form-group col-md-3">
<label for="Desk Phone" class="col-form-label">DESK PHONE</label>
<input class="form-control" id="deskNumber"
ng-class="{validbr: showdn==='false', invalidbr: showdn }"
ng-focus="showdn=false"
ng-blur="showdn=userForm.deskNumber.$invalid" name="deskNumber"
ng-model="user.deskNumber" ng-minlength="14" maxlength="14"
phone-input placeholder="Enter Desk Phone #" />
<div style="color: Red"
ng-show="showdn && userForm.deskNumber.$error.minlength">Error
- Need 10 digits for a Phone #</div>
<div style="color: Red"
ng-show="showdn && userForm.deskNumber.$error.maxlength">Error
- Need 10 digits for a Phone #</div>
</div>
<div class="form-group">
<!-- <input type ="hidden" ng-model="user.createdBy" name="createdBy" />
<input type ="hidden" ng-model="user.updatedBy" name="updatedBy" /> -->
</div>
<!--
<input type="hidden" ng-value="user.createdBy" name="createdBy" />
<input type="hidden" ng-value="user.updatedBy" name="updatedBy" /> -->
</div>
</div>
</div>
</div>
</form>

ng-form in ng-repeat and checking the validation status of all forms

So, I have a parent form with a nested set of ng-forms like this:
<form class="row" name="parentForm" ng-repeat="model in controller.addresses track by $index" novalidate>
<div class="col-xs-12 row-title">
<h1>{{ model.type || 'Delivery' }} address</h1>
</div>
<div class="col-xs-12 col-sm-4 col-lg-4">
<div class="form" name="saveForm" ng-form>
<div class="form-group">
<label class="control-label">Company name</label>
<input class="form-control" name="company" type="text" placeholder="Enter your company name" ng-model="model.company" />
</div>
<div class="form-group" ng-class="{ 'has-error' : saveForm.address1.$invalid && !saveForm.address1.$pristine }">
<label class="control-label">House name/number</label>
<input class="form-control" name="houseName" type="text" placeholder="Please enter your address 1" ng-model="model.houseName" ng-minlength="3" required />
</div>
<div class="form-group">
<label class="control-label">Street</label>
<input class="form-control" name="street" type="text" placeholder="Please enter your address 2" ng-model="model.street" />
</div>
<div class="form-group">
<label class="control-label">Town</label>
<input class="form-control" name="town" type="text" placeholder="Please enter your address 3" ng-model="model.town" />
</div>
<div class="form-group">
<label class="control-label">County</label>
<input class="form-control" name="county" type="text" placeholder="Please enter your address 4" ng-model="model.county" />
</div>
<div class="form-group" ng-class="{ 'has-error' : saveForm.postCode.$invalid && !saveForm.postCode.$pristine }">
<label class="control-label">Post code</label>
<input class="form-control" name="postCode" type="text" placeholder="Enter your post code" ng-model="model.postCode" ng-minlength="3" required />
</div>
</div>
</div>
</form>
I then have a button:
<div class="row">
<div class="col-xs-12 col-sm-4 col-lg-4">
<div div class="row">
<div class="col-xs-6 tile">
<a class="red" ui-sref="^">
<i class="fa fa-trash"></i>
<div class="footer">Back</div>
</a>
</div>
<div class="col-xs-6 tile" ng-if="parentForm.$valid">
<a class="yellow" ui-sref="^.finance">
<i class="fa fa-arrow-right"></i>
<div class="footer">Continue</div>
</a>
</div>
</div>
</div>
</div>
I want this button to only show if all child forms are valid. I was hoping that I could just use parentForm.$valid but that doesn't work.
Does anyone know how to solve this?
Try to do parentForm.saveForm.$valid.
It will work
Nested forms aren't valid in HTML5 - but you can place your saveForm outside the parentForm and then use the input element's form attribute to specify a form for your input elements.
<form name="form1">
<input type="string" ng-model="form1input" required>
<input type="string" form="form2" ng-model="form2input" required>
</form>
<div ng-form="form2" id="form2"></div>
<button ng-if="form1.$valid && form2.$valid">Click</button>
Plunker: https://plnkr.co/edit/y9ipsNoEW596guLf2CzM?p=preview

Resources