2

Is there a way to use angular input validation without form. See angular plunker. When I change <form name="myForm"> by <div name="myForm"> the validation does not work anymore.

HTML :

<form name="myForm">
  <label>
     User name:
     <input type="text" name="userName" ng-model="user.name" required>
  </label>
  <div role="alert">
    <span class="error" ng-show="myForm.userName.$error.required">
     Required!</span>
  </div>
  <label>
     Last name:
     <input type="text" name="lastName" ng-model="user.last" ng-minlength="3" ng-maxlength="10">
  </label>
  <div role="alert">
    <span class="error" ng-show="myForm.lastName.$error.minlength">Too short!</span>
    <span class="error" ng-show="myForm.lastName.$error.maxlength">Too long!</span>
  </div>
</form>
6
  • What is the reason to remove the form? Commented Aug 4, 2016 at 9:21
  • My real use of input is inside an input component with angular.module('myModule').component('myInput') which will be called in a parent form component and I don't want to put a form in a form Commented Aug 4, 2016 at 9:25
  • @Mouss So you just want an input validation? ng-model makes also validation possible... Commented Aug 4, 2016 at 9:27
  • @Mikey ng-model doesn't respond with my needs because I have like 20 angular directive for specific input form validation and I don't want to rewrite all those validation directives with ng-model Commented Aug 4, 2016 at 9:29
  • 1
    you can use ng-form directive in div. This will work. check the link plnkr.co/edit/Z090iiwrinMoFHfP2vyA?p=preview Commented Aug 4, 2016 at 9:40

1 Answer 1

4

You need to have form directive because ngModel searches its controller in order to register itself and leverage validation capabilities.

If for layout reasons you can't have form tag (nested <form> tags are invalid) then you can use ngForm directive to achieve the same effect:

<ng-form name="myForm">
  <label>
     User name:
     <input type="text" name="userName" ng-model="user.name" required>
  </label>
  <div role="alert">
    <span class="error" ng-show="myForm.userName.$error.required">
     Required!</span>
  </div>
  <label>
     Last name:
     <input type="text" name="lastName" ng-model="user.last"
     ng-minlength="3" ng-maxlength="10">
  </label>
  <div role="alert">
    <span class="error" ng-show="myForm.lastName.$error.minlength">
      Too short!</span>
    <span class="error" ng-show="myForm.lastName.$error.maxlength">
      Too long!</span>
  </div>
</ng-form>

Demo: https://plnkr.co/edit/WlCqNBWtqiGerkQy0Wad?p=preview

Sign up to request clarification or add additional context in comments.

2 Comments

Thanx @dfsq, when you say > ngModel searches its controller in order to register itself and leverage validation capabilities. You mean ngModel search form's controller?
I mean <input ng-model="asdfa" name="something">. So ngModel directive searches for ngForm directive waling up the DOM tree. When it find it it registers itself with it. Something like ngFormController.addControl(...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.