AngularJS uses angular-formly for Form Verification and angularjs for form verification.

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.