AngularJS使用angular-formly進(jìn)行表單驗(yàn)證
當(dāng)驗(yàn)證表單中有很多字段時(shí),這時(shí)候可能希望把html的生成以及驗(yàn)證邏輯放到controller中,在頁面,也許是這樣的:
<some-form fiedls="vm.someFields" ...></some-form>
然后,在controller中定義各個(gè)字段以及驗(yàn)證。angular-formly就是為這個(gè)需求而存在。
在controller中,把各個(gè)字段定義在數(shù)組中:
vm.rentalFields = [
{
key:'first_name',
type:'input',
templateOptions:{
type:'text',
label:'姓',
placeholder: '輸入姓',
required: true
}
},
...
]
使用hideExpression字段定義隱藏的條件:
{
key:'under18',
type:'checkbox',
templateOptions:{
label:'是否不滿18歲'
},
hideExpression: '!model.email' //email驗(yàn)證失敗之前不顯示
}
使用validators字段自定義驗(yàn)證規(guī)則:
{
key:'license',
type:'input',
templateOptions:{
label:'身份證號(hào)',
placeholder:'輸入身份證號(hào)'
},
hideExpression: '!model.province',
validators:{
driversLicense: function($viewValue, $modelValue, scope){
var value = $modelValue || $viewValue;
if(value){
return validateDriversLicence(value);
}
},
expressionProperties:{
'templateOptions.disabled':function($viewValue, $modelValue, scope){
if(scope.model.province == '山東省'){
return false;
}
return true;
}
}
}
首先安裝:npm install angular-formly angular-formly-templates-bootstrap bootstrap api-check
Demo的文件結(jié)構(gòu):
css/
.....style.css
node_modules/
scripts/
.....MainController.js
.....provinces.js [提供select的選項(xiàng),有關(guān)省]
app.js
index.html
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="gb2312"> <title></title> <link rel="stylesheet" href="css/style.css"/> <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css"/> </head> <body ng-app="formlyApp" ng-controller="MainController as vm"> <div class="container col-md-4 col-md-offset-4"> <form novalidate> <formly-form model="vm.rental" fields="vm.rentalFields" form="vm.rentalForm"> <button type="submit" class="btn btn-primary" ng-disabled="vm.rentalForm.$invalid">提交</button> </formly-form> </form> </div> <!--項(xiàng)目依賴--> <script src="node_modules/api-check/dist/api-check.js"></script> <script src="node_modules/angular/angular.min.js"></script> <script src="node_modules/angular-formly/dist/formly.js"></script> <script src="node_modules/angular-formly-templates-bootstrap/dist/angular-formly-templates-bootstrap.min.js"></script> <!--項(xiàng)目引用--> <script src="app.js"></script> <script src="scripts/MainController.js"></script> <script src="scripts/province.js"></script> </body> </html>
app.js
(function(){
'use strict';
angular.module('formlyApp',['formly','formlyBootstrap'])
})();
province.js
以factory的方式返回一個(gè)對(duì)象,包含獲取select選項(xiàng)的方法。
(function(){
'use strict';
angular
.module('formlyApp')
.factory('province', province);
function province(){
function getProvinces(){
return [
{
"name":"山東省",
"value":"山東省"
},
{
"name":"江蘇省",
"value":"江蘇省"
}
];
}
return {
getProvinces:getProvinces
}
}
})();
MainController.js
(function(){
'use strict';
angular
.module('formlyApp')
.controller('MainController', MainController);
function MainController(province){
var vm = this;
vm.rental = {};
vm.rentalFields = [
{
key:'first_name',
type:'input',
templateOptions:{
type:'text',
label:'姓',
placeholder: '輸入姓',
required: true
}
},
{
key:'last_name',
type:'input',
templateOptions:{
type:'text',
label:'名',
placeholder:'輸入名',
required:true
}
},
{
key:'email',
type:'input',
templateOptions:{
type:'email',
label:'郵箱',
placeholder:'輸入郵箱',
required:true
}
},
{
key:'under18',
type:'checkbox',
templateOptions:{
label:'是否不滿18歲'
},
hideExpression: '!model.email' //email驗(yàn)證失敗之前不顯示
},
{
key: 'province',
type:'select',
templateOptions:{
label:'選擇省',
options: province.getProvinces()
},
hideExpression: '!model.email'
},
{
key:'license',
type:'input',
templateOptions:{
label:'身份證號(hào)',
placeholder:'輸入身份證號(hào)'
},
hideExpression: '!model.province',
validators:{
driversLicense: function($viewValue, $modelValue, scope){
var value = $modelValue || $viewValue;
if(value){
return validateDriversLicence(value);
}
},
expressionProperties:{
'templateOptions.disabled':function($viewValue, $modelValue, scope){
if(scope.model.province == '山東省'){
return false;
}
return true;
}
}
}
},
{
key: 'insurance',
type: 'input',
templateOptions:{
label:'保險(xiǎn)',
placeholder:'輸入保險(xiǎn)'
},
hideExpression: '!model.under18 || !model.province'
}
];
function validateDriversLicence(value) {
return /[A-Za-z]\d{4}[\s|\-]*\d{5}[\s|\-]*\d{5}$/.test(value);
}
}
})();
以上內(nèi)容是小編給大家分享的AngularJS使用angular-formly進(jìn)行表單驗(yàn)證的全部敘述,希望大家喜歡。
相關(guān)文章
詳解angular臟檢查原理及偽代碼實(shí)現(xiàn)
這篇文章主要介紹了詳解angular臟檢查原理及偽代碼實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
Angular 4依賴注入學(xué)習(xí)教程之簡(jiǎn)介(一)
依賴注入式AngularJS的重要特性之一,依賴注入簡(jiǎn)化了Angular解析模塊/組件之間依賴的過程。下面這篇文章主要給大家介紹了關(guān)于Angular 4依賴注入基礎(chǔ)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
angular + express 實(shí)現(xiàn)websocket通信
最近需要實(shí)現(xiàn)一個(gè)功能,后端通過TCP協(xié)議連接雷達(dá)硬件的控制器,前端通過websocket連接后端,當(dāng)控制器觸發(fā)消息的時(shí)候,把信息通知給所以前端,本文給的大家講解angular + express 實(shí)現(xiàn)websocket通信的思路,感興趣的朋友一起看看吧2023-09-09
從?Angular?Route?中提前獲取數(shù)據(jù)的方法詳解
這篇文章主要介紹了從?Angular?Route?中提前獲取數(shù)據(jù),通過本文,你將學(xué)會(huì)使用?resolver,?在?Angular?App?中應(yīng)用?resolver,應(yīng)用到一個(gè)公共的預(yù)加載導(dǎo)航,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
AngularJS基礎(chǔ) ng-hide 指令用法及示例代碼
本文主要介紹AngularJS ng-hide 指令,這里整理了ng-hide指令的基礎(chǔ)資料,并附實(shí)例代碼,有興趣的小伙伴參考下2016-08-08
AngularJS中的Directive自定義一個(gè)表格
本篇文章給大家介紹在angularjs中自定義一個(gè)有關(guān)表格的directive,涉及到angularjs directive相關(guān)知識(shí),對(duì)本文感興趣的朋友一起學(xué)習(xí)吧2016-01-01

