1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Unable to read disabled field value in validator. But form shows error

Discussão em 'Angular' iniciado por Mani Nalliappan, Outubro 25, 2024 às 09:13.

  1. when initially loaded form , disabled form fields are getting error on the form validation . Also I tried to get disabled field didn't can't value. like control.value, control.getRaWValue, control.get("value").

    const data={option :1, text1:"foo", text2:"Bee",text3:"Baz"}

    HTML Template :

    <select name="options" [(ngModel)]="data.option">
    <option value="1"> option1</options>
    <option value="2"> option2</options>
    <option value="3"> option3</options>
    </select>

    <input type="text" [(ngModel)]="data.text1" [disable]="data.option !==1" appdirective1>
    <input type="text" [(ngModel)]="data.text2" [disable]="data.option !==2" appdirective1>
    <input type="text" [(ngModel)]="data.text2" [disable]="data.option !==3">




    @Directive({
    selector: '[appdirective1]',
    })
    export class directiveName implements OnChanges, OnInit {

    constructor(private _injector: Injector) {}

    validate1(): ValidatorFn {
    return (control: AbstractControl): ValidationErrors | null => {
    //logic here
    return control.value === ' '
    ? {
    invalidName: "field is required",
    }
    : null;
    };
    }

    ngOnInit() {
    this.conditionallyAddValidators();
    }

    conditionallyAddValidators() {
    const control = this._injector.get(NgControl);
    control?.control?.addValidators(this.validate1());

    control?.control?.updateValueAndValidity();
    }

    ngOnChanges() {
    this.conditionallyAddValidators();
    }
    }


    Even I am getting error after form loading data. without change any data

    Continue reading...

Compartilhe esta Página