Skip to content

Instantly share code, notes, and snippets.

@RazorNd
Created August 3, 2023 17:42
Show Gist options
  • Save RazorNd/77cb2a8b94c63d5a04c414382e1979df to your computer and use it in GitHub Desktop.
Save RazorNd/77cb2a8b94c63d5a04c414382e1979df to your computer and use it in GitHub Desktop.
async validator
import { Component } from '@angular/core';
import { FormArray, FormControl, FormGroup, Validators } from "@angular/forms";
@Component({
selector: 'app-reactive-form',
templateUrl: './reactive-form.component.html',
styleUrls: ['./reactive-form.component.scss']
})
export class ReactiveFormComponent {
genders = ['male', 'female', 'other']
hobbies = new FormArray<FormControl<string | null>>([])
signUpForm = new FormGroup({
'userData': new FormGroup({
'username': new FormControl(null, Validators.required),
'email': new FormControl(null, {
validators: [Validators.required, Validators.email],
asyncValidators: [this.uniqueEmailValidator.validate.bind(this.uniqueEmailValidator)]
})
}),
'gender': new FormControl('female'),
'hobbies': this.hobbies
})
get hobbyControls(): Array<FormControl<string | null>> {
return this.hobbies.controls
}
onSubmit() {
console.log(this.signUpForm)
}
onValidation(path: string): boolean {
return (!this.signUpForm.get(path)?.valid && this.signUpForm.get(path)?.touched) ?? true
}
onAddHobby() {
this.hobbies.push(new FormControl('', [Validators.required]))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment