Question

Hello anyone has anyone tried doing a validation using angularjs in a with Hot Towel template?

Basically i have a property in my angular controller which is binded(two way) in my view.

I just want to do a simple required validation, and then show a <span> element with the message.

here is my controller code

(function () {
    'use strict';
    var controllerId = 'login';
    angular.module('app').controller(controllerId, ['$scope', 'common', 'userservice','$location', login]);

    function login($scope, common, userservice, $location) {
        var getLogFn = common.logger.getLogFn;
        var log = getLogFn(controllerId);

        var vm = this;
        vm.title = 'Login';

        //view model for credentials
        vm.email = null; 
        activate();

        function activate() {
            common.activateController([], controllerId)
                .then(function () { log('Activated Login View'); });
        }

    }
})();

and this is my view

<div data-ng-controller="login as vm">
    <form name="loginform" id="loginform" novalidate data-ng-submit="loginuser()">
        <fieldset>
            <legend>Login</legend>
            <p>
                <label>Email</label>
                <input type="email" data-ng-model="email" placeholder="Email" required />
                <span  data-ng-show="loginform.email.$error.required">*</span>
            </p>


        </fieldset>
    </form>
</div>

i dont know what the problem is, but the <span> just wont show. am i missing something?

Was it helpful?

Solution

You have to set 'name' attribute to your input email :

<input type="email" name="email" data-ng-model="email" placeholder="Email" required />

For each field of your form, angularjs will set a value like: loginform[name-attribute].

For your information, your span will be visible only when required error is triggered (when your email is not empty, your span will be hidden).

[EDIT] See this fiddle: http://jsfiddle.net/k82at

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top