Skip to content

Instantly share code, notes, and snippets.

@matthieu-D
Created February 25, 2017 10:53
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 matthieu-D/f3a3b6b8eb2576d82326cc9f0b8d6915 to your computer and use it in GitHub Desktop.
Save matthieu-D/f3a3b6b8eb2576d82326cc9f0b8d6915 to your computer and use it in GitHub Desktop.
import { Component } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
@Component({
selector: 'my-home',
templateUrl: './home.component.html'
})
export class HomeComponent {
userForm: FormGroup;
constructor (formBuilder: FormBuilder) {
this.userForm = formBuilder.group({
name: '',
email: '',
address: formBuilder.group({
country: '',
city: '',
})
});
}
ngOnInit() {
const nameControl = this.userForm.controls["name"];
nameControl.valueChanges.subscribe(value => {
if (value.trim() === '') {
nameControl.setErrors({
required: true
});
}
});
}
onFormSubmitted(form) {
console.log('form value:', form);
// Rest of the logic here
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment