これは、angular の最新バージョンを使用し、AngularJS の検証フレームワークを使用するカスタム検証ディレクティブを構築する方法です。
ステップ 1: スタイルを追加する
これは見た目を良くするため...
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<style>
ul {
margin: 0;
padding:0;
}
</style>
ステップ 2: AngularJS ライブラリを追加する
AngularJS のコア ライブラリと ngMessages が必要です。
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular-messages.min.js"></script>
ステップ 3: 検証ディレクティブを定義する
はい、「必須」および「最大長」の定義済みの AngularJS ディレクティブが既にあることは知っていますが、これは SO の回答であり、自分で定義する方法を知っていることが重要です。
app.directive('rqrd', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
ngModel.$validators.rqrd = function(modelValue, viewValue) {
var value = modelValue || viewValue;
return value != null && value != undefined && value != '';
}
}
}
});
app.directive('max', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
var maxValue = parseInt(attr.max);
ngModel.$validators.max = function(modelValue, viewValue) {
var value = modelValue || viewValue;
return value.length > 0 && value.length <= maxValue;
}
}
}
});
app.directive('startsWith', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
ngModel.$validators.startsWith = function(modelValue, viewValue) {
var value = modelValue || viewValue;
return value && value.length > 0 && value.indexOf(attr.startsWith) == 0;
}
}
}
});
ステップ 4: 検証ディレクティブを使用する
<form name="form">
Favorite Color <br />
<input name="color" ng-model="color" rqrd max="10" />
<ul ng-messages="form.color.$error">
<li ng-message="rqrd" class="label label-danger">This is required</li>
<li ng-message="max" class="label label-danger">Max 10 chars</li>
</ul>
Name <br />
<input name="name" ng-model="name" starts-with="Z" />
<ul ng-messages="form.name.$error">
<li ng-message="rqrd" class="label label-danger">Please enter an Age</li>
<li ng-message="startsWith" class="label label-danger">Must start with Z</li>
</ul>
</form>
フォームが有効であることの確認
if ($scope.form.$valid) {
...
}
var app = angular.module('app', ['ngMessages']);
app.controller('ctrl', function($scope) {
});
app.directive('rqrd', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
ngModel.$validators.rqrd = function(modelValue, viewValue) {
var value = modelValue || viewValue;
return value != null && value != undefined && value != '';
}
}
}
});
app.directive('max', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
var maxValue = parseInt(attr.max);
ngModel.$validators.max = function(modelValue, viewValue) {
var value = modelValue || viewValue;
return value.length > 0 && value.length <= maxValue;
}
}
}
});
app.directive('startsWith', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attr, ngModel) {
ngModel.$validators.startsWith = function(modelValue, viewValue) {
var value = modelValue || viewValue;
return value && value.length > 0 && value.indexOf(attr.startsWith) == 0;
}
}
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.6/angular-messages.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<style>
ul {
margin: 0;
padding:0;
}
</style>
<h3>Example heading </h3>
<div ng-app="app" ng-controller="ctrl">
<form name="form">
Favorite Color <br />
<input name="color" ng-model="color" rqrd max="10" />
<ul ng-messages="form.color.$error">
<li ng-message="rqrd" class="label label-danger">This is required</li>
<li ng-message="max" class="label label-danger">Max 10 chars</li>
</ul>
Name <br />
<input name="name" ng-model="name" starts-with="Z" />
<ul ng-messages="form.name.$error">
<li ng-message="rqrd" class="label label-danger">Please enter an Age</li>
<li ng-message="startsWith" class="label label-danger">Must start with Z</li>
</ul>
</form>
</div>