11import { autoinject } from 'aurelia-framework' ;
2+ import { ValidationControllerFactory , ValidationController , validateTrigger , ValidationRules } from 'aurelia-validation' ;
23
34import { LogManager , Logger } from './../../services/logger.service' ;
45import { AppConfigService } from './../../services/app-config.service' ;
@@ -7,26 +8,48 @@ import { LanguageService } from './../../services/language.service';
78@autoinject
89export class Welcome {
910 private logger : Logger ;
11+ private vController : ValidationController ;
1012
1113 public heading : string = 'Welcome to the Aurelia Navigation App' ;
1214 public firstName : string = 'John' ;
1315 public lastName : string = 'Doe' ;
1416 public previousValue : string = this . fullName ;
1517 public currentDate : Date = new Date ( ) ;
1618 public jsonProperty : Object = { key1 : 'value1' , key2 : 'value2' } ;
19+ public validationValid : boolean = false ;
1720
1821 constructor (
1922 private appConfigService : AppConfigService ,
20- private languageService : LanguageService
23+ private languageService : LanguageService ,
24+ validationControllerFactory : ValidationControllerFactory
2125 ) {
2226 this . logger = LogManager . getLogger ( 'Welcome VM' ) ;
2327 this . logger . info ( 'appConfig => name:' , appConfigService . getName ( ) ) ;
2428 this . logger . info ( 'appConfig => version:' , appConfigService . getVersion ( ) ) ;
2529 this . logger . info ( 'appConfig => env:' , appConfigService . getEnv ( ) ) ;
2630 this . logger . info ( 'appConfig => platform:' , appConfigService . getPlatform ( ) ) ;
2731 this . logger . info ( 'appConfig => config:' , appConfigService . getConfig ( ) ) ;
32+
33+ this . vController = validationControllerFactory . createForCurrentScope ( ) ;
34+ this . vController . validateTrigger = validateTrigger . manual ;
35+ }
36+
37+ public canDeactivate ( ) : boolean {
38+ if ( this . fullName !== this . previousValue ) {
39+ return confirm ( 'Are you sure you want to leave?' ) ;
40+ }
41+ return true ;
2842 }
2943
44+ public validateFirstName ( ) : void {
45+ this . vController
46+ . validate ( {
47+ object : this ,
48+ rules : ValidationRules . ensure ( 'firstName' ) . required ( ) . rules
49+ } )
50+ . then ( r => this . validationValid = r . valid ) ;
51+ }
52+
3053 //Getters can't be directly observed, so they must be dirty checked.
3154 //However, if you tell Aurelia the dependencies, it no longer needs to dirty check the property.
3255 //To optimize by declaring the properties that this getter is computed from, uncomment the line below
@@ -41,13 +64,6 @@ export class Welcome {
4164 alert ( `Welcome, ${ this . fullName } !` ) ;
4265 }
4366
44- public canDeactivate ( ) : boolean {
45- if ( this . fullName !== this . previousValue ) {
46- return confirm ( 'Are you sure you want to leave?' ) ;
47- }
48- return true ;
49- }
50-
5167 public switchLanguage ( ) : void {
5268 const lang = this . languageService . getCurrentLang ( ) ;
5369 if ( lang === this . languageService . getSupportedLanguages ( ) [ 0 ] ) {
0 commit comments