是否可以创建一个可以使用多个值来确定我的字段是否有效的验证器?
例如,如果客户的首选联系方式是通过电子邮件,则应该要求电子邮件字段.
谢谢.
更新了示例代码...
import {Component, View} from 'angular2/angular2';
import {FormBuilder, Validators, formDirectives, ControlGroup} from 'angular2/forms';
@Component({
selector: 'customer-basic',
viewInjector: [FormBuilder]
})
@View({
templateUrl: 'app/components/customerBasic/customerBasic.html',
directives: [formDirectives]
})
export class CustomerBasic {
customerForm: ControlGroup;
constructor(builder: FormBuilder) {
this.customerForm = builder.group({
firstname: [''],
lastname: [''],
validateZip: ['yes'],
zipcode: ['', this.zipCodeValidator]
// I only want to validate using the function below if the validateZip control is set to 'yes'
});
}
zipCodeValidator(control) {
if (!control.value.match(/\d\d\d\d\d(-\d\d\d\d)?/)) {
return { invalidZipCode: true };
} …Run Code Online (Sandbox Code Playgroud) 关于验证器与angular2的比较字段,请参阅此问题.不幸的是Angular 2改变了一点,所以解决方案似乎不再起作用了.这是我的代码:
import {IonicApp,Page,NavController,NavParams} from 'ionic/ionic'
import {Component} from 'angular2/core'
import {FORM_PROVIDERS, FormBuilder, Validators} from 'angular2/common'
import {ControlMessages} from '../../components/control-messages'
import {ValidationService} from '../../services/validation-service'
@Page({
templateUrl: 'build/pages/account/register.html',
directives: [ControlMessages]
})
export class RegisterPage {
constructor(nav:NavController,private builder: FormBuilder) {
this.nav = nav
this.registerForm = this.builder.group({
'name' : ['', Validators.required],
'email' : ['',Validators.compose([Validators.required, ValidationService.emailValidator])],
'password' : ['',Validators.required],
'repeat' : ['',this.customValidator]
}
)
}
register() {
alert(this.registerForm.value.password)
}
private customValidator(control) {
//console.log(this.registerForm.value.password)
//return {isEqual: control.value === this.registerForm.value.password} …Run Code Online (Sandbox Code Playgroud) 我想只使用材料组件执行密码和确认密码验证 ,如果和.确认许多资源无法实现,则在确认密码字段下面显示错误消息.confirm password field doesn't matchif it is empty
也试过这个视频.
这是我正在寻找的材料成分
HTML
<mat-form-field >
<input matInput placeholder="New password" [type]="hide ? 'password'
: 'text'" [formControl]="passFormControl" required>
<mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility' :
'visibility_off'}}</mat-icon>
<mat-error *ngIf="passFormControl.hasError('required')">
Please enter your newpassword
</mat-error>
</mat-form-field>
<mat-form-field >
<input matInput placeholder="Confirm password" [type]="hide ?
'password' : 'text'" [formControl]="confirmFormControl"
required>
<mat-icon matSuffix (click)="hide = !hide">{{hide ? 'visibility' :
'visibility_off'}}</mat-icon>
<mat-error *ngIf="confirmFormControl.hasError('required')">
Confirm your …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Angular2中创建一个具有"重复密码"功能的注册表单.我希望这可以使用自定义验证器作为表单控件.
我遇到的问题是,当验证器运行时,"this-context"似乎被设置为验证器,因此我无法访问RegistrationForm类上的任何本地方法.我似乎无法找到从验证器访问ControlGroup的任何好方法.
任何人都知道在自定义验证器中访问同一控件组中的其他控件的好方法吗?
这是组件的简短示例:
import { Component, View } from 'angular2/angular2';
import { Validators, ControlGroup, Control, FORM_DIRECTIVES, FORM_BINDINGS } from 'angular2/angular2';
@Component({
selector: 'form-registration'
})
@View({
directives: [FORM_DIRECTIVES, ROUTER_DIRECTIVES],
template: `
<form (submit)="register($event)" [ng-form-model]="registerForm">
<label for="password1">Password:</label>
<input id="password1" ng-control="password1" type="password" placeholder="Passord" />
<label for="password2">Repeat password:</label>
<input id="password2" ng-control="password2" type="password" placeholder="Gjenta passord" />
<button class="knapp-submit" type="submit" [disabled]="!registerForm.valid">Registrer deg</button>
</form>
`
})
export class RegistrationForm {
registerForm: ControlGroup;
constructor() {
this.registerForm = new ControlGroup({
password1: new Control('', Validators.required),
password2: new Control('', this.customValidator) …Run Code Online (Sandbox Code Playgroud) 我正在构建一个 Angular 5 表单。我需要在我的表格(电子邮件或电话)上提供联系信息。需要电子邮件地址或电话号码,但不能同时提供。我将如何为这种情况创建自定义验证器?从文档来看,验证器似乎只负责一个控件,而我的验证器需要知道多个控件来检查它们的所有值。
ngOnInit() {
this.form = this.formBuilder.group({
'name': [null, [Validators.required]],
'email': [null, []], // A user can provide email OR phone, but
'phone': [null, []], // both are not required. How would you do this?
});
}
Run Code Online (Sandbox Code Playgroud) 我的密码问题和在我的角应用程序中确认密码有问题.我正在使用被动表单,错误显示"提供的参数与呼叫目标上的任何签名都不匹配"
ngOnInit() {
this.form = this.formBuilder.group({
name: [null, [Validators.required, Validators.minLength(3)]],
email: [null, [Validators.required, Validators.email]],
password: [null, Validators.required],
confirm_password: [null, Validators.required],
}, {validator: this.passwordConfirming('password', 'confirm_password')});
}
passwordConfirming(c: AbstractControl): { invalid: boolean } {
if (c.get('password').value !== c.get('confirm_password').value) {
return {invalid: true};
}
}
Run Code Online (Sandbox Code Playgroud)
HTML
<div class="form-inline">
<label class="col-md-4">Password</label>
<input class="col-md-8" type="password" class="form-control" id="password" formControlName="password">
<span class="text-muted" *ngIf="!form.controls['password'].valid && form.controls['password']?.touched"> Password is required</span>
</div>
<div class="form-inline">
<label class="col-md-4">Confirm Password</label>
<input class="col-md-8" type="password" class="form-control" id="confirm_password" formControlName="confirm_password">
</div>
Run Code Online (Sandbox Code Playgroud)