您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何操作Angular实现模版驱动表单的自定义校验功能

2025/7/17 22:04:33发布34次查看
这次给大家带来如何操作angular实现模版驱动表单的自定义校验功能,操作angular实现模版驱动表单的自定义校验功能的注意事项有哪些,下面就是实战案例,一起来看一下。
html5原生的表单校验属性(必填,长度限制,取值间隔,正则表达式等等)可以满足普通的校验需求,但是有些场景必须用到自定义校验,比如注册时的密码确认,有比对关系的时间/数值选择, 需要到请求到服务端取值验证等等···这里以密码确认为例进行说明。
指令开发
表单的验证状态是通过 formcontro l的 errors 属性反馈出来的,所以基本的思路肯定就是需要添加校验规则,然后将验证结果添加到formcontrol实例的errors属性中。那么问题来了,模版驱动表单的控制都是在html模版中完成的,无法直接接触到 formcontrol实例。这个时候就需要使用指令了,将检验规则进行包装。angular提供了 验证器供应商 ng_validators ,用于处理表单自定义校验。先创建指令。
import { directive} from '@angular/core'; import { ng_validators, validator, abstractcontrol} from '@angular/forms'; @directive({  selector: '[appconfirmpsw]',  providers: [{   provide : ng_validators,   useexisting : confirmpswdirective,   multi: true  }] }) export class confirmpswdirective implements validator {  constructor() {   }  validate(control: abstractcontrol): {[key: string]: any} {    //检验规则  } }
1、为指令指定供应商  ng_validators , 和别名类 confirmpswdirective , 及 multi 为true(可以用同一个token,注册不同的 provide)。因为是在 ng_validators 提供商中注册的指令,所以才能被angular的验证流程识别,需要注意的是要用useexisting来注册,这样就不会创建一个新的实例。
2、用 validator接口来约束 自定义的指令,这是angular提供的验证器的类 。有validate属性,会传入表单的formcontrol,返回 validationerrors 对象。
现在指令结构完成,开始进行校验部分。首先需要传入已输入的密码,所以增加@input,再指定校验规则,判断绑定表单的值和传入的已输入值是否相同
@input('appconfirmpsw') confirmpsw: string;
为了避免使用指令时,还需要额外传入confirmpsw属性 ( <input type="password" appconfirmpsw [confirmpsw]="'xxx'" >),所以我们将 指令名称appconfirmpsw作为confirmpsw的别名,这样传值会比较方便,简化为  <input type="password" [appconfirmpsw] = "'xxx'">。
这里专门写一个检验函数,用来比对值和返回结果。记得在指令的validate中调用一下
export function comfirmpswvalidator(_confirmpsw: string): validatorfn { //传入已输入的密码值 , 返回一个validatorfn  return (control: abstractcontrol): {[key: string]: any} => { //传入绑定表单的formcontrol   if ( !control.value ) { //如果绑定未输入值,则返回 required错误    return { 'required' : true };   } //如果两次输入的值不相同,则返回confirmpsw的错误   return control.value !== _confirmpsw ? {'confirmpsw' : {value: true}} : null;  }; }
完整指令如下:
import { directive, input } from '@angular/core'; import { ng_validators, validator, abstractcontrol, validatorfn} from '@angular/forms'; @directive({  selector: '[appconfirmpsw]',  providers: [{   provide : ng_validators,   useexisting : confirmpswdirective,   multi: true  }] }) export class confirmpswdirective implements validator {  @input('appconfirmpsw') confirmpsw: string;  constructor() {   }  validate(control: abstractcontrol): {[key: string]: any} {   console.log(this.confirmpsw);   return this.confirmpsw ? comfirmpswvalidator(this.confirmpsw)(control) : null;  } } export function comfirmpswvalidator(_confirmpsw: string): validatorfn {  return (control: abstractcontrol): {[key: string]: any} => {   if ( !control.value ) {    return { 'required' : true };   }   return control.value !== _confirmpsw ? {'confirmpsw' : {value: true}} : null;  }; }
使用
测试一下指令的效果吧
<p class="input-group">    <label class="group-label" for="psw-new"> 新密码 :</label>    <input class="group-input" [(ngmodel)]="inputpsw.new" #new="ngmodel" type="password" name="psw" id="psw-new" required>   </p>   <p class="input-group input-error" *ngif="new.touched&&new.invalid">    <p class="group-error-content" *ngif="new.errors?.required">确认密码为必填项!</p>   </p>   <p class="input-group">    <label class="group-label" for="psw-confirm">确认密码 :</label>    <input class="group-input" [(ngmodel)]="inputpsw.confirm" #confirm="ngmodel" type="password" name="confirm" id="psw-confirm" [appconfirmpsw] = "new.value" required>   </p>   <p class="input-group input-error" *ngif="confirm.touched&&confirm.invalid">    <p class="group-error-content" *ngif="confirm.errors?.required">新密码为必填项!</p>    <p class="group-error-content" *ngif="confirm.errors?.confirmpsw">密码输入不一致!</p>   </p>
传入new表单的值,并通过errors.confirmpsw属性来控制提示语反馈。密码输入不一致,可以正确的校验到
确认密码为空时的提示也正确
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
如何处理mac安装thrift因bison报错
如何使用淘宝镜像cnpm安装vue.js
以上就是如何操作angular实现模版驱动表单的自定义校验功能的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product