Angular:设置自定义错误和编辑字段的方法

在表单中,每当出现错误时,清除文本字段并按如下所示设置错误

e['body']

为避免重复行,编写了如下方法

switch(result){
    case "SUCCESS":
        // success case
    case "ERROR1":
            this.Form.controls.text1.setvalue('');
            this.Form.controls.text2.setvalue('');
            this.Form.controls.text1.setErrors({ 'error1': true });
            break;
    case "ERROR2":
            this.Form.controls.text1.setvalue('');
            this.Form.controls.text2.setvalue('');
            this.Form.controls.text1.setErrors({ 'error2': true });
            break;
    case "ERROR3":
            this.Form.controls.text1.setvalue('');
            this.Form.controls.text2.setvalue('');
            this.Form.controls.text1.setErrors({ 'error3': true });
            break;
}

字段正在清除,但无法设置错误。有关如何将特定错误设置为true的任何建议。

heishashengzhe111 回答:Angular:设置自定义错误和编辑字段的方法

从您的问题中我推断出什么是问题,您正在尝试清除字段并将错误设置为formControls。

要在字段中设置错误,

public function search($term,$limit)
{
    $builder = Product::select([
        'id','title','image','price','sale_price'
    ])->active();

    $builder = app(Pipeline::class)
        ->send($builder)
        ->through([
            ShipsTo::class,])
        ->thenReturn();

    return $builder->paginate($limit);
}

要重置表单中的所有字段,

this.Form.get('controlName').setErrors({notUnique: true});

此外,如果您想使用自定义验证程序来验证表单,请参考以下链接: https://alligator.io/angular/reactive-forms-custom-validator/

本文链接:https://www.f2er.com/3118166.html

大家都在问