gpt4 book ai didi

Angular 2+ Material mat-chip-list formArray验证

转载 作者:行者123 更新时间:2023-12-05 06:55:45 27 4
gpt4 key购买 nike

如何验证 mat-chip 是否已添加到 mat-chip-list。我正在使用 ReactiveForms。我已经尝试使用 required 验证器。

该值可以是姓名列表,因此在提交表单之前,我需要确保我的姓名列表中至少有 1 个姓名。如果列表为空,则 mat-error 应该显示错误消息。使用 required 验证器会使表单无效,无论将名称添加到列表中。

编辑:响应式表单

我尝试制作一个自定义验证器,我现在使用的是响应式表单而不是模板驱动的表单,但我无法让它工作。我编辑了以下代码以反射(reflect)我的更改,并创建了这个 https://stackblitz.com/edit/angular-4d5vfj

HTML

<form [formGroup]="myForm">
<mat-form-field class="example-chip-list">
<mat-chip-list #chipList formArrayName="names">
<mat-chip *ngFor="let name of myForm.get('names').controls; let i=index;"
[formGroupName]="i"
[selectable]="selectable"
[removable]="removable"
(removed)="remove(myForm, i)">
<mat-icon matChipRemove *ngIf="removable">cancel</mat-icon>
</mat-chip>

<input placeholder="Names"
[matChipInputFor]="chipList"
[matChipInputSeparatorKeyCodes]="separatorKeysCodes"
[matChipInputAddOnBlur]="addOnBlur"
(matChipInputTokenEnd)="add($event, asset)">
</mat-chip-list>
<mat-error>Atleast 1 name need to be added</mat-error>
</mat-form-field>
</form>

TS

import {COMMA, ENTER} from '@angular/cdk/keycodes';
import {Component} from '@angular/core';
import {FormGroup, FormControl, FormBuilder, FormArray} from '@angular/forms';
import {MatChipInputEvent} from '@angular/material';

@Component({
selector: 'chip-list-validation-example',
templateUrl: 'chip-list-validation-example.html',
styleUrls: ['chip-list-validation-example.css'],
})
export class ChipListValidationExample {
public myForm: FormGroup;

// name chips
visible = true;
selectable = true;
removable = true;
addOnBlur = true;
readonly separatorKeysCodes: number[] = [ENTER, COMMA];

// data
data = {
names: ['name1', 'name2']
}

constructor(private fb: FormBuilder) {
this.myForm = this.fb.group({
names: this.fb.array(this.data.names, this.validateArrayNotEmpty)
});
}

initName(name: string): FormControl {
return this.fb.control(name);
}

validateArrayNotEmpty(c: FormControl) {
if (c.value && c.value.length === 0) {
return {
validateArrayNotEmpty: { valid: false }
};
}
return null;
}

add(event: MatChipInputEvent, form: FormGroup): void {
const input = event.input;
const value = event.value;

// Add name
if ((value || '').trim()) {
const control = <FormArray>form.get('names');
control.push(this.initName(value.trim()));
console.log(control);
}

// Reset the input value
if (input) {
input.value = '';
}
}

remove(form, index) {
console.log(form);
form.get('names').removeAt(index);
}
}

最佳答案

问题是当 chipList 的 FormArray 状态为 INVALID 时,chipList 的 errorState 未设置为 true >。

我面临着同样的问题,不知道为什么这不是开箱即用的,也不知道如何将 chipList 的表单作为 FormArray 来隐式实现。

作为解决方法,您可以从 FormArray 监听状态变化并手动设置 chipList 的 errorState:

@ViewChild('chipList') chipList: MatChipList;

ngOnInit() {
this.myForm.get('names').statusChanges.subscribe(
status => this.chipList.errorState = status === 'INVALID'
);
}

https://stackblitz.com/edit/angular-4d5vfj-gywxjz

关于Angular 2+ Material mat-chip-list formArray验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65307864/

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