Created
February 25, 2017 10:53
-
-
Save matthieu-D/f3a3b6b8eb2576d82326cc9f0b8d6915 to your computer and use it in GitHub Desktop.
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
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