Skip to content

Instantly share code, notes, and snippets.

@willmendesneto
Created March 19, 2015 17:58
Show Gist options
  • Save willmendesneto/196f84c884cf501dfced to your computer and use it in GitHub Desktop.
Save willmendesneto/196f84c884cf501dfced to your computer and use it in GitHub Desktop.
AngularJS: testing form validation
// In your controller

this.item = {
    userType: ''
};
<!-- In your html template -->

<form name="myForm" class="my-form" role="form" novalidate ng-submit="vm.save(vm.item)" >
    <a href="#" ng-click="register(myForm)">aaa</a>
    userType: <input type="text" name="userType" ng-model="vm.item.userType" required />

    <input type="submit" value="SAVE" ng-disabled="myForm.$invalid && !myForm.data.$dirty" />
    <div ng-if="mainCtrl.item.userType !== '' && mainCtrl.item.userType !== undefined && myForm.userType.$invalid && myForm.userType.$dirty && !myForm.userType.$focused">
        <p class="help-block">
          This field is required
        </p>
    </div>

    <div class="col-lg-12" ng-if="myForm.$invalid && myForm.data.$dirty">
        <p>You shall not pass =/</p>
        <p>You only can finish the operation if all fields required was populated correctly</p>

        <p>userType = {$ userType $}</p><br/>
        <p>myForm.input.$valid = {$ myForm.input.$valid$}</p><br/>
        <p>myForm.input.$error = {$ myForm.input.$error$}</p><br/>
        <p>myForm.$valid = {$ myForm.$valid $}</p><br/>
        <p>myForm.$error.required = {$!!myForm.$error.required$}</p><br/>
    </div>
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment