AngularJS uses angular-formly for Form Verification and angularjs for form verification.
When there are many fields in the verification form, you may want to put the html generation and verification logic in the controller. On the page, it may be like this:
<some-form fiedls="vm.someFields" ...></some-form>
Then, define each field in the controller and verify it. Angular-formly exists for this requirement.
In the controller, define fields in the array:
Vm. export alfields = [{key: 'First _ name', type: 'input', templateOptions: {type: 'text', label: 'surname ', placeholder: 'input surname ', required: true }},...]
Use the hideExpression field to define the hidden conditions:
{Key: 'under18', type: 'checkbox', templateOptions: {label: 'under 18 '}, hideExpression :'! Model. email '// not displayed before email verification fails}
Use the validators field to customize verification rules:
{Key: 'license ', type: 'input', templateOptions: {label: 'id card No.', placeholder: 'enter ID card No. '}, 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 = 'shandong province ') {return false;} return true ;}}}
First install: npm install angular-formly-templates-bootstrap api-check
Demo file structure:
Css/
.....Style.css
Node_modules/
Scripts/
... MainController. js
... Provinces. js [select options are provided, related provinces]
App. js
Index.html
Index.html
<! DOCTYPE html>
App. js
(function(){'use strict';angular.module('formlyApp',['formly','formlyBootstrap'])})();
Province. js
Return an object in the form of factory, including the method for obtaining the select option.
(function(){'use strict';
Angular
. Module ('formlyapp '). factory ('province ', province); function province () {function getProvinces () {return [{"name": "Shandong province", "value": "Shandong province "}, {"name": "Jiangsu Province", "value": "Jiangsu Province"}] ;}return {getProvinces: getProvinces }}})();
MainController. js
(Function () {'use strict '; angular. module ('formlyapp '). controller ('maincontroller', MainController); function MainController (province) {var vm = this; vm. marshal ={}; vm. export alfields = [{key: 'First _ name', type: 'input', templateOptions: {type: 'text', label: 'surname ', placeholder: 'input surname ', required: true },{ key: 'Last _ name', type: 'input', templateOptions: {type: 'text', label: 'name', placeholder: 'input name', required: true },{ key: 'email', Type: 'input', templateOptions: {type: 'email ', label: 'mailbox', placeholder: 'input mailbox ', required: true },{ key: 'under18', type: 'checkbox', templateOptions: {label: 'under 18 '}, hideExpression :'! Model. email '// not displayed before email verification fails}, {key: 'vince', type: 'select', templateOptions: {label: 'select province ', options: province. getProvinces ()}, hideExpression :'! Model. email '}, {key: 'license', type: 'input', templateOptions: {label: 'id card No. ', placeholder: 'enter ID card No.'}, 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 = 'shandong province ') {return false;} return true ;}}}, {key: 'Insurance', type: 'input', templateOptions: {label: 'policy', placeholder :' Input insurance '}, hideExpression :'! Model. under18 |! Model. province '}]; function validateDriversLicence (value) {return/[A-Za-z] \ d {4} [\ s | \-] * \ d {5} [\ s | \-] * \ d {5} $ /. test (value );}}})();
The above content is a summary of AngularJS's use of angular-formly for form verification. I hope you will like it.
Articles you may be interested in:
- AngularJS implements Form Verification
- Detailed explanation of Form Verification programming in AngularJS
- Details about how AngularJS implements Form Verification
- AngularJs implements ng1.3 + Form Verification
- AngularJS uses ngMessages for Form Verification