Skip to content

Instantly share code, notes, and snippets.

@rexar1988
Last active October 2, 2018 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rexar1988/190c6f2e0a4ce018dfce312b9de96cb1 to your computer and use it in GitHub Desktop.
Save rexar1988/190c6f2e0a4ce018dfce312b9de96cb1 to your computer and use it in GitHub Desktop.
Angular: Reactive Forms and form validate
<form [formGroup]="form" (submit)="onSubmit()">
<input type="text" formControlName="title">
<button type="submit" [disabled]="form.invalid">Сохранить объявление</button>
</form>
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