7

I have a simple page with validating the field, something like this: employee.html and its validating the field but if I have added the same code and called the same page using ng-include the validating is not working

without ng-include validate works:

 <form name="form" class="form-horizontal" ng-validate="create(data)" novalidate>
   <div class="row">
       <div class="col-xs-6">
           <div class="form-group">
               <label for="" class="col-xs-6 control-label">First Name:</label>
                  <div class="col-xs-6">
                     <input name="first_name" type="text" class="form-control" ng-model="data.first_name" placeholder="First name" ng-required="true">
                        <div class="alert alert-danger" ng-show="form.$submitted && form.first_name.$error.required">Enter first name.</div>
                  </div>
            </div>
       </div>
   </div>
</form>

with ng-include validate does not works:

dashboard.html

<div class="container">
     ....................
     ..................
     <ng-include src="employee.html"></ng-include>         
</div>

my question is: how can I make the validation works within ng-include?

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406

3 Answers3

0

yes your $submitted is not working angular version 1.3. if you need to show the message if only form is submit then you can keep a variable to detect whether the form is submitted or not.

to do that

in controller test function

$scope.create = function(data) {
     $scope.formSubmitted = true;
}

in validation message use formSubmitted instead of $submitted

<div class="alert alert-danger" ng-show="formSubmitted && form.first_name.$error.required">E..

here is the working Plunker


But if you are using angular 1.3 or later version

angular introduce a $submitted in angular 1.3.x and may be your trying with a older angular version, upgrading angular will solve your problem.

here is the Changes of the 1.3 - please check the

new feature: Add new $submitted state to forms

<div class="alert alert-danger" ng-show="form.$submitted && form.first_name.$error.required">Enter first name.</div>

here is the working plunker

Kalhan.Toress
  • 21,683
  • 8
  • 68
  • 92
0

You can use bootstrap validation in ui like that

<form name="personalinformation">

<label for="fname">  First Name<span style="color:red" ng-show="personalinformation.firstname.$error.required">*</span></label>  
<input type="text" class="form-control" id="" placeholder="First    Name" ng-model="PerInfo.FirstName" required name="firstname">

</form>

and if you validation by angular you can create validationFunction and ng-click pass the value of text or other element in controller.If input type is false then you cal fill value in ng-show="true"

Sundar
  • 142
  • 1
  • 15
0

Use <ng-form></ng-form> instead of <form></form>

Kenath
  • 600
  • 5
  • 13