Last active
June 14, 2017 06:14
-
-
Save Takas0522/7e1c29cffe6b9ec679132aff86b7fd30 to your computer and use it in GitHub Desktop.
Angular v4 ReactiveFormのやつ
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
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