Skip to content

Instantly share code, notes, and snippets.

@Takas0522
Last active June 14, 2017 06:14
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 Takas0522/7e1c29cffe6b9ec679132aff86b7fd30 to your computer and use it in GitHub Desktop.
Save Takas0522/7e1c29cffe6b9ec679132aff86b7fd30 to your computer and use it in GitHub Desktop.
Angular v4 ReactiveFormのやつ
export class BaseComponent {
inputForm: FormGroup;
formErrors: any;
validationMessage: any;
buildForms() {
this.inputForm.valueChanges.subscribe(data => {
this.onValueChange(data);
});
}
onValueChange(data?: any) {
const form = this.inputForm;
for(const field in this.formErrors){
/* Initialize form's Errors */
this.formErrors[field] = '';
const control = form.get(field);
if (control && control.dirty && !control.valid) {
const messages = this.validationMessage[field];
for (const key in control.errors){
this.formErrors[field] += messages[key];
}
this.emitErrorAction(field);
}
if (control && control.dirty && control.valid) {
this.emitNotErrorAction(field);
}
}
}
emitErrorAction(field: any) {
/* エラー発生時処理、継承先で実装 */
}
emitNotErrorAction(field: any){
/* エラー未発生時処理、継承先で実装 */
}
canDeActivateInputPage(): boolean {
return confirm("ページを離れてもいい??");
}
allControlReCheck() {
for (const field in this.formErrors) {
const control = this.inputForm.controls[field];
control.markAsDirty();
this.onValueChange();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment