gpt4 book ai didi

validation - 如何将验证器动态添加到 Angular 2 中的 FormControl

转载 作者:太空狗 更新时间:2023-10-29 17:02:33 25 4
gpt4 key购买 nike

我正在使用 ReactiveFormsModule 并在常量配置中定义了我所有的表单控件,包括简单的验证器,例如 Validators.required

我想向其中一个 FormControl 添加一个自定义验证器。

我目前已将自定义验证器添加为此配置中的一个函数,它工作正常,但它不属于这里,它确实需要存在于我的组件中,但我不确定如何附加在 FormBuilder 配置我的所有控件后手动自定义验证器。

请参阅下面的代码注释

How do I Attach Here

* ??? *

this.form.get('site_id').add custom valiator

这是我当前的配置代码。

import {FormControl, Validators, FormBuilder} from '@angular/forms';

var fb = new FormBuilder();

function exampleValidator(control: FormControl): { [s: string]: boolean} {
if (control.value === 'Example'){
return { example: true };
}

return null;
}

export const formConfig = fb.group({
'extract_batch_id': ['bbbbbbbbbbbbb',
[
Validators.required
]],
'site_id': ['blah',
[
Validators.required,
exampleValidator
]]
});

我有一个真正应该存储自定义验证器的指令

求职组件

import {Component, Input, OnInit, OnDestroy} from '@angular/core';
import {FormGroup, FormControl} from '@angular/forms';
import {ActivatedRoute} from '@angular/router';
import {Subscription} from 'rxjs';

import {Job} from '../../../models/job';
import {JobService} from '../../../services/api/job.service';
import {DebugService} from '../../../common/debug/debug.service';
import {formConfig} from './edit.form-config';

@Component({
selector: 'wk-job-search-edit',
template: require('./edit.html')
})
export class JobSearchEditComponent {
form: FormGroup;

job: Job;

@Input() jobId: number;
private subscription: Subscription;

constructor(
private route: ActivatedRoute,
private jobService: JobService,
private debug: DebugService){

// Configure form FormGroup via exported formConfig
this.form = formConfig;

// How do I Attach Here
// *** ??? ***
// this.form.get('site_id').add custom valiator
}

/*
exampleValidator(control: FormControl): { [s: string]: boolean} {
if (control.value === 'Example'){
return { example: true };
}

return null;
}
*/
}

JobSearch Edit.html

<form [formGroup]="form" (ngSubmit)="onSubmit()">

<button type="submit" class="btn btn-success" [disabled]="!form.valid">Save</button>
<div class="form-group" [ngClass]="{'has-danger':!form.get('extract_batch_id').valid}">
<label for="extract_batch_id" class="form-control-label">Extract Batch</label>
<input id="extract_batch_id" formControlName="extract_batch_id" type="text" placeholder="Extract Batch" class="form-control input-sm">
<div *ngIf="!form.get('extract_batch_id').valid">
<div class="form-control-feedback">Extract Batch is required?</div>
<small class="form-text text-muted">Please enter a Extract Batch, eg. xyz.</small>
</div>
</div>

<div class="form-group" [ngClass]="{'has-danger':!form.get('site_id').valid}">
<label for="site_id" class="form-control-label">Site</label>
<input id="site_id" formControlName="site_id" type="text" placeholder="Site" class="form-control input-sm">
<div *ngIf="!form.get('site_id').valid">
<div class="form-control-feedback">Site is required?</div>
<small class="form-text text-muted">Please enter a Site, eg. xyz.</small>
</div>
</div>


</form>

最佳答案

查看您的代码,您可以做什么:

<div class="form-group" [ngClass]="{'has-danger':!form.get('site_id').valid}">
<label for="site_id" class="form-control-label">Site</label>
<input id="site_id" [formControl]="site_id_control" type="text" placeholder="Site" class="form-control input-sm">
<div *ngIf="!form.get('site_id').valid">
<div class="form-control-feedback">Site is required?</div>
<small class="form-text text-muted">Please enter a Site, eg. xyz.</small>
</div>
</div>

查看[formControl]="site_id_control"

然后,通过这种方式,您可以像这样从该特定控件添加或删除验证器:

在你的类里面:

export class JobSearchEditComponent {
private site_id_control=this.form.controls['site_id'];

updateValidator(){
let exisitingValidators = this.site_id_control.validators;
this.site_id_control.setValidators(Validators.compose([...existingValidators , exampleValidator]))

// you probably also need this :
this.site_id_control.updateValueAndValidity();

}
}

关于validation - 如何将验证器动态添加到 Angular 2 中的 FormControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41175411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com