I want to create custom component for input text but I don't know how can I bind validation of each input field to custom component. Is there any way to set errors of each field as array of objects like below
<app-custom-input
formControlName="username"
label="Username"
[errors]="[
{ type: 'required', message: 'is required' },
{ type: 'minlength', message: 'min length error' }
]"
></app-custom-input>
You did most of the stuff already. The only missing thing is to correctly implement to your needs the
validatefunction inside the custom component.Angular will take care of validating the control when the return array has length 0, while you can format the
ValidationErrorsshape to your needs.Here is your code updated, with the current
control.errorand validity displayed below the input.Sidenote: It would be nice to reference a
ValidatorFnto validate the code instead of checking the control value itself, but unfortunately I was not able to do that.