I am trying to do a custom validation for my form. I have a service that checks in my database if the reference is valid.
Once the reference has been checked, I assign a boolean value to a variable, depending on that value I would like to validate or not the form.
The controller code is:
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
dataForm: FormGroup;
controlref: boolean;
constructor(private fb: FormBuilder) {
}
ngOnInit() {
this.dataForm = this.fb.group({
ref: ['', [Validators.required, this.validateRef(this.controlref)]],
})
}
changeControlRef(article: boolean) {
this.controlref = !this.controlref;
console.log('aqui',this.controlref)
}
validateRef(controlref: boolean) {
return () => {
if (controlref) {
return { 'validRef': true };
} else {
return { 'validRef': false };
}
}
}
}
I leave here the STACKBLITZ
// The changeControlRef () function simulates the assignment of my service.