Skip to content

Commit 6703e8b

Browse files
committed
add validation example
1 parent d6c130f commit 6703e8b

File tree

7 files changed

+44
-10
lines changed

7 files changed

+44
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,5 @@ Typedoc is generated with command ```npm run build:docs``` into directoy ```docs
203203
- [ ] Add electron
204204
- [X] Add pipline example
205205
- [X] Add utility converters https://www.npmjs.com/package/aurelia-utility-converters
206-
- [ ] Add validation example
206+
- [X] Add validation example
207207
- [X] Add configure http client example

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"aurelia-templating-binding": "^1.2.0",
9090
"aurelia-templating-resources": "^1.2.0",
9191
"aurelia-templating-router": "^1.0.0",
92+
"aurelia-validation": "^1.0.0-beta.1.0.1",
9293
"bootstrap-sass": "^3.3.7",
9394
"font-awesome": "^4.7.0",
9495
"i18next-browser-languagedetector": "^1.0.1",

src/app/app.vm.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ export class AppViewModel {
2929
this.configureHttpClient();
3030
}
3131

32+
public attached(): void {
33+
this.configureMoment();
34+
}
35+
3236
public configureRouter(config: RouterConfiguration, router: Router): void {
3337
config.title = this.i18n.tr('TITLE');
3438
if (this.appConfigService.platformIsBrowser()) {

src/app/main.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ export async function configure(aurelia: Aurelia): Promise<void> {
7070
}
7171
});
7272
})
73+
/**
74+
* Aurelia Validation plugin
75+
* See: https://github.com/aurelia/validation
76+
*
77+
* Configure i18n for aurelia-validation error messages.
78+
* See: http://aurelia.io/hub.html#/doc/article/aurelia/validation/latest/validation-basics
79+
*/
80+
.plugin('aurelia-validation')
7381
// Uncomment the line below to enable animation.
7482
// .plugin('aurelia-animator-css');
7583
// if the css animator is enabled, add swap-order="after" to all router-view elements

src/app/modules/welcome/welcome.vm.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,9 @@ <h3>Switch Language</h3>
3535
<h3>Converter utility</h3>
3636
<p>${ jsonProperty | json }</p>
3737
<p>Available converters are: filter, json, limit, md5 and sort</p>
38+
39+
<h3>Validation</h3>
40+
<p>Validation is: ${validationValid}</p>
41+
<button class="btn btn-primary" click.delegate="validateFirstName()">validate</button>
3842
</section>
3943
</template>

src/app/modules/welcome/welcome.vm.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { autoinject } from 'aurelia-framework';
2+
import { ValidationControllerFactory, ValidationController, validateTrigger, ValidationRules } from 'aurelia-validation';
23

34
import { LogManager, Logger} from './../../services/logger.service';
45
import { AppConfigService } from './../../services/app-config.service';
@@ -7,26 +8,48 @@ import { LanguageService } from './../../services/language.service';
78
@autoinject
89
export 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]) {

webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ module.exports = function (envArguments) {
8282
'aurelia-templating',
8383
'aurelia-templating-binding',
8484
'aurelia-templating-router',
85-
'aurelia-templating-resources'
85+
'aurelia-templating-resources',
86+
'aurelia-validation'
8687
],
8788
theme: [
8889
'bootstrap-sass'

0 commit comments

Comments
 (0)