Gaurav Gupta
Gaurav Gupta

Reputation: 1925

AngularJS form validation "required" not working

I am having problem with angular validation.
this one does'nt work...

<input type="text" ng-model="text1" name="text1" required>
<span ng-show="text1.$error.required">Please enter something!</span>

but this works:

<form name="myform">
<input type="text" ng-model="text1" name="text1" required>
<span ng-show="myform.text1.$error.required">Please enter something!</span>
</form>

is it possible to somehow correct the first one without placing it inside a form?

thanks

Upvotes: 3

Views: 5679

Answers (2)

brute_force
brute_force

Reputation: 1171

You can use the "ng-form" directive if you really dont want to add a form tag.

<body ng-controller="MainCtrl">
  <div ng-form="myForm">
    <input type="text" required ng-model="user.name" placeholder="Username">
    <button ng-click="doSomething()" ng-disabled="myForm.$invalid">DO</button>
  </div>
</body>

example

Upvotes: 3

Anid Monsur
Anid Monsur

Reputation: 4538

As far as I know, no, it is not possible. The FormController is what handles the states of each form element, so you need a reference to it in order to check the validation state.

Upvotes: 1

Related Questions