How to organize better validation flow? (Control value accesor with custom validators)

70 Views Asked by At

I want that at the input of value it was validated and passed further or not.

      onChange(event: any) {
        this._onChange(event.target);
        this._onTouched();
      }

     validate({ value }: any): ValidationErrors | null {
        if (!value.name) {

          return null;
        }

        const validator =  this._getValidatorByName(value.name) || null;
        const error = validator ? validator(value) : null;

        if (!error) {

          this.updateOption(value.name, value.value);
        } else {

          this._onChange(null);
        }

        return error;
      }

Can I remove the pass logic from the validate method?

I mean how we can listen to errors inside the controller, without putting side-effect code inside the validate method?

            if (!error) {

              this.updateTimeOption(value.name, value.value);
            } else {

              this._onChange(null);
            }
0

There are 0 best solutions below