Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ngMessages/angular validation not working

Tags:

angularjs

I'm getting neither a value emitted by the template fragment nor functioning validation. My Jade template:

doctype html
html(lang='en', ng-app="Validate")
  head
    script(src='#{cdn_path}/angular.js/1.3.11/angular.js')
    script(src='#{cdn_path}/angular.js/1.3.11/angular-messages.js')
    script.
      angular.module('Validate', ['ngMessages']);
  body
    .container
      form(method="POST", action="/apply", name="myform", novalidate="")
        pre myform.name.$error = {{ myform.name.$error }}
        input.form-control(name="name", required="", pattern=".*[a-zA-Z].*", minlength=5)
        ng-messages(for="myform.name.$error")
          ng-message(when="required") Required!
          ng-message(when="min") Too small!
        input.btn(type='submit')

The resulting HTML: http://plnkr.co/edit/McyMXwW1b2Ae7kkwQ1sP

I'd like to avoid custom directives or much in the way of additional Javascript. What am I doing wrong?

like image 252
Allen Luce Avatar asked Feb 03 '15 16:02

Allen Luce


1 Answers

This is happening because you didn't bind ng-model with the input :-)

 <input name="name" ng-model="name" required="" pattern=".*[a-zA-Z].*" minlength="5" class="form-control">
        <ng-messages for="myform.name.$error">
        <ng-message when="minlength">Too small!</ng-message>
        <ng-message when="required">Required!</ng-message>

Plunker:- http://plnkr.co/edit/kmNkfhdVOHQsstj6dpPT?p=preview

like image 78
squiroid Avatar answered Sep 23 '22 14:09

squiroid