Last active
October 2, 2018 20:05
-
-
Save rexar1988/190c6f2e0a4ce018dfce312b9de96cb1 to your computer and use it in GitHub Desktop.
Angular: Reactive Forms and form validate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form [formGroup]="form" (submit)="onSubmit()"> | |
<input type="text" formControlName="title"> | |
<button type="submit" [disabled]="form.invalid">Сохранить объявление</button> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
form: FormGroup; | |
constructor(private _fb: FormBuilder) {} | |
ngOnInit() { | |
this.form = new FormGroup({ | |
title: new FormControl('', [Validators.required, Validators.maxLength(75)]), | |
}); | |
// FormBuilder an approach | |
this.form = this._fb.group({ | |
title: [null, [ | |
Validators.required, | |
Validators.minLength(5), | |
Validators.maxLength(255), | |
Validators.pattern('^[^ ][0-9a-zA-Zа-яА-Я() ]*') | |
]] | |
}); | |
} | |
onSubmit() { | |
console.log(this.form); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment