Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material 0.9.7 Form Validation Example

I want to validate form using angularjs and angular-material built-in directives including messages under field like, input, select, radio buttons.
There are some specific requirements:

  • The Form fields should have equal heights
  • The radio buttons i.e. md-radio should be inline
  • The location of messages under fields should be similar
  • The angular-material select i.e. md-select should be width = 100%
like image 669
Salal Aslam Avatar asked Jun 07 '15 18:06

Salal Aslam


2 Answers

I did some tweaks to Salal Aslam answer:

  • submit button is enable
  • erorr messages aren't shown on untouched form
  • validate on submit
  • validate on touching element

Example on JSFiddle and below.

HTML:

<form name="myForm" ng-app="myApp" ng-controller="myController" class="container-fluid" ng-submit="myForm.$valid && submit()" novalidate>
  <div class="row">
    <div class="col-xs-8">
      <md-input-container md-is-error="myForm.name.$invalid && (myForm.$submitted || myForm.name.$dirty)">
        <label>Name</label>
        <input name="name" id="name" ng-model="obj.name" ng-required="true">
        <div ng-messages="myForm.name.$error" ng-if="myForm.$submitted || myForm.name.$touched">
          <div ng-message="required">Campaign Name is required.</div>
        </div>
      </md-input-container>
    </div>
    <div class="col-xs-8">
      <md-input-container  md-is-error="myForm.myselect.$invalid && (myForm.$submitted || myForm.myselect.$dirty)">
        <md-select name="myselect" id="myselect" placeholder="myselect" ng-model="obj.myselect" ng-required="true">
          <md-option ng-repeat="o in options" ng-value="o">{{o}}</md-option>
        </md-select>
        <div ng-messages="myForm.myselect.$error"  ng-if="myForm.$submitted || myForm.myselect.$touched">
          <div ng-message="required">myselect is required.</div>
        </div>
      </md-input-container>
    </div>
    <div class="col-xs-8">
      <md-input-container md-is-error="myForm.status.$invalid && (myForm.$submitted || myForm.status.$dirty)">
        <md-radio-group name="status" id="status" ng-model="obj.status" ng-required="true" class="">
          <md-radio-button ng-repeat="s in statuses" ng-value="s">{{s}}</md-radio-button>
        </md-radio-group>
        <div ng-messages="myForm.status.$error"  ng-if="myForm.$submitted || myForm.status.$touched">
          <div ng-message="required">status is required.</div>
        </div>
      </md-input-container>
    </div>
  </div>
  <md-button type="button" ng-click="reset()">RESET</md-button>
  <md-button type="submit" class="md-primary">SUBMIT</md-button>
</form>

JS:

var app = angular
  .module('myApp', ['ngAnimate', 'ngAria', 'ngMaterial', 'ngMessages'])
  .controller('myController', function ($scope) {
    $scope.statuses = ['Planned', 'Confirmed', 'Cancelled'];
    $scope.options = ['Option 1', 'Option 2', 'Option 3', 'Option 4', '...'];
    $scope.submit = function() {
      // submit code goes here
    };
    $scope.reset = function() {
      $scope.obj = {
        name: "",
        myselect: "",
        status: ""
      }
    };
    $scope.reset();
  });
like image 117
Styx Avatar answered Nov 14 '22 20:11

Styx


I created this fiddle for the requirements mentioned. I am posting this here to help someone new to angularjs or angular-material

<form name="myForm" ng-app="myApp" ng-controller="myController" class="container-fluid" ng-submit="submit()">
  <div class="row">
    <div class="col-xs-8">
      <md-input-container>
        <label>Name</label>
        <input name="name" id="name" ng-model="obj.name" ng-required="true">
        <div ng-messages="myForm.name.$error">
          <div ng-message="required">Campaign Name is required.</div>
        </div>
      </md-input-container>
    </div>
    <div class="col-xs-8">
      <md-input-container>
        <md-select name="myselect" id="myselect" placeholder="myselect" ng-model="obj.myselect" ng-required="true">
          <md-option ng-repeat="o in options" ng-value="o">{{o}}</md-option>
        </md-select>
        <div ng-messages="myForm.myselect.$error">
          <div ng-message="required">myselect is required.</div>
        </div>
      </md-input-container>
    </div>
    <div class="col-xs-8">
      <md-input-container>
        <md-radio-group name="status" id="status" ng-model="obj.status" ng-required="true" class="">
          <md-radio-button ng-repeat="s in statuses" ng-value="s">{{s}}</md-radio-button>
        </md-radio-group>
        <div ng-messages="myForm.status.$error">
          <div ng-message="required">myselect is required.</div>
        </div>
      </md-input-container>
    </div>
  </div>
  <md-button type="button" ng-click="reset()">RESET</md-button>
  <md-button class="md-primary" ng-disabled="myForm.$invalid">SUBMIT</md-button>
</form>


var app = angular
  .module('myApp', ['ngAnimate', 'ngAria', 'ngMaterial', 'ngMessages'])
  .controller('myController', function ($scope) {
    $scope.statuses = ['Planned', 'Confirmed', 'Cancelled'];
    $scope.options = ['Option 1', 'Option 2', 'Option 3', 'Option 4', '...'];
    $scope.submit = function() {
      // submit code goes here
    };
    $scope.reset = function() {
      $scope.obj = {
        name: "",
        myselect: "",
        status: ""
      }
    }
    $scope.reset();
  });


md-input-container > md-select {
    margin-top: 0;
    padding-bottom: 0; }
md-input-container > md-select > md-select-label {
    width: 100%; }
md-input-container > md-radio-group { 
    padding: 24px 2px 0; }
md-input-container > md-radio-group > md-radio-button { 
    margin: 8px 5px 0;
    display: inline-block; }

fiddle link

Update 1: Created this pen for angular 1.4.2 and angular-material v0.10.0

like image 28
Salal Aslam Avatar answered Nov 14 '22 22:11

Salal Aslam