gpt4 book ai didi

angular - 如何使用 angular2 设置验证器不接受索引 0 的选定选项

转载 作者:搜寻专家 更新时间:2023-10-30 22:01:00 24 4
gpt4 key购买 nike

我使用这样的选择选项

<div class="form-group row" [ngClass]="{'has-error': (!form.controls['blockFirstIndex'].valid && form.controls['blockFirstIndex'].touched), 'has-success': (form.controls['blockFirstIndex'].valid && form.controls['blockFirstIndex'].touched)}">
<label class="col-sm-3 control-label">blockFirstIndex</label>
<div class="col-sm-9">
<select formControlName="blockFirstIndex" [(ngModel)]="value" class="form-control">
<option *ngFor="let item of items" [disabled]="item.id==0" [ngValue]="item">{{item.name}}</option>
</select>
</div>
</div>

我的验证器

this.form = this.fb.group({
'blockFirstIndex': ['', Validators.compose([Validators.required])],
});

如何让验证器不接受索引 0 的选择选项?

最佳答案

Form controller could be like this:

blockFirstIndex: new FormControl("", [
SelectionValidator.isValidSelection,
Validators.required
])

and you can define vlaidator like this:

import {FormControl} from '@angular/forms';
export class SelectionValidator {
static isValidSelection(control: FormControl){
if (control.value === "" || control.value=== "0") {
return { "Please provide a valid selection": true };
}
return null;
}

}

关于angular - 如何使用 angular2 设置验证器不接受索引 0 的选定选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41711090/

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