AngularJS <input> validation with no enclosing <form>

AngularjsValidation

Angularjs Problem Overview


Is it possible in Angular to validate a single, isolated <input> in a similar way the forms are validated? I'm thinking about something like this:

<div class="form-group">
    <input name="myInput" type="text" class="form-control" ng-model="bindTo" ng-maxlength="5">
    <span class="error" ng-show="myInput.$error.maxlength">Too long!</span>
</div>

The example above doesn't work. Enclosing it in a <form> and replacing ng-show with ng-show="myForm.myInput.$error.maxlength" helps.

Is it possible to do this without using <form>?

Angularjs Solutions


Solution 1 - Angularjs

You may use the ng-form angular directive (see docs here) to group anything, even outside a html form. Then, you can take advantage from angular FormController.

<div class="form-group" ng-form name="myForm">
    <input name="myInput" type="text" class="form-control" ng-model="bindTo" ng-maxlength="5">
    <span class="error" ng-show="myForm.myInput.$error.maxlength">Too long!</span>
</div>

Example

Solution 2 - Angularjs

Building on Silvio Lucas' answer, if you are iterating in a loop and need to be able to interpolate form names and valid states:

<div
  name="{{propertyName}}"
  ng-form=""
  class="property-edit-view"
  ng-class="{
    'has-error': {{propertyName}}.editBox.$invalid,
    'has-success':
      {{propertyName}}.editBox.$valid &&
      {{propertyName}}.editBox.$dirty &&
      propertyValue.length !== 0
  }"
  ng-switch="schema.type">
  <input
    name="editBox"
    ng-switch-when="int"
    type="number"
    ng-model="propertyValue"
    ng-pattern="/^[0-9]+$/"
    class="form-control">
  <input
    name="editBox"
    ng-switch-default=""
    type="text"
    ng-model="propertyValue"
    class="form-control">
  <span class="property-type" ng-bind="schema.type"></span>
</div>

Solution 3 - Angularjs

<!DOCTYPE html>
<html ng-app="plunker">
<head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js">   </script>

</head>

<body ng-controller="MainCtrl">
  	<div class="help-block error" ng-show="test.field.$error.required">Required</div>
  	<div class="help-block error" ng-show="test.firstName.$error.required">Name Required</div>
    <p>Hello {{name}}!</p>
    <div ng-form="test" id="test">
      	<input type="text" name="firstName" ng-model="firstName" required> First name <br/>	
        <input id="field" name="field" required ng-model="field2" type="text"/>
    </div>
</body>
<script>
  	var app = angular.module('plunker', []);

	app.controller('MainCtrl', function($scope) {
	  $scope.name = 'World';
	  $scope.field = "name";
	  $scope.firstName = "FirstName";
	  $scope.execute = function() {
	    alert('Executed!');
	  }
	});

</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWojtekView Question on Stackoverflow
Solution 1 - AngularjsSilvio LucasView Answer on Stackoverflow
Solution 2 - AngularjsBlaskoviczView Answer on Stackoverflow
Solution 3 - Angularjsmaniac coderView Answer on Stackoverflow