Last active
December 19, 2016 14:06
-
-
Save Thanood/43dc539ffe5d302b922a2abc14600b48 to your computer and use it in GitHub Desktop.
Aurelia-Materialize bridge datepicker validation
This file contains hidden or 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
| <template> | |
| <md-card md-title="Basic"> | |
| <div> | |
| <input md-datepicker="container: body; value.two-way: date & validate:rules;" md-datepicker.ref="datePicker" type="date" placeholder="pick a date"> | |
| </div> | |
| <div style="margin-top: 15px;"> | |
| <button md-waves md-button click.delegate="validateModel()">validate</button> | |
| <button md-waves md-button click.delegate="reset()">reset</button> | |
| </div> | |
| </md-card> | |
| <md-card md-title="Show or hide error text"> | |
| <div> | |
| <input md-datepicker="show-errortext.bind: showErrortext; container: body; value.two-way: date2 & validate:rules;" md-datepicker.ref="date2Picker" type="date" placeholder="pick a date"> | |
| </div> | |
| <div style="margin-top: 15px;"> | |
| <button md-waves md-button click.delegate="validateModel2()">validate</button> | |
| <button md-waves md-button click.delegate="reset2()">reset</button> | |
| </div> | |
| <md-switch style="margin-top: 15px;" md-label-on="show" md-label-off="hide" md-checked.bind="showErrortext"></md-switch> | |
| </md-card> | |
| <md-card if.bind="controller.errors.length"> | |
| <h5 class="error-text">You have errors!</h5> | |
| <ul style="margin-top: 15px;"> | |
| <li repeat.for="error of controller.errors"> | |
| <a href="#" click.delegate="openErrorTarget(error, $event)"> | |
| ${error.message} | |
| </a> | |
| </li> | |
| </ul> | |
| </md-card> | |
| </template> |
This file contains hidden or 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 { inject, NewInstance } from 'aurelia-framework'; | |
| import { ValidationController, ValidationRules } from 'aurelia-validation'; | |
| import { MaterializeFormValidationRenderer } from 'aurelia-materialize-bridge'; | |
| @inject(NewInstance.of(ValidationController)) | |
| export class App { | |
| date = null; | |
| date2 = null; | |
| showErrortext = false; | |
| controller = null; | |
| rules = ValidationRules | |
| .ensure('date') | |
| .displayName('Date') | |
| .required() | |
| .ensure('date2') | |
| .displayName('Show or hide error text date') | |
| .required() | |
| .on(this) | |
| .rules; | |
| constructor(controller: ValidationController) { | |
| this.controller = controller; | |
| this.controller.addRenderer(new MaterializeFormValidationRenderer()); | |
| } | |
| reset() { | |
| this.date = null; | |
| this.controller.reset({ object: this, propertyName: 'date' }); | |
| } | |
| reset2() { | |
| this.date2 = null; | |
| this.controller.reset({ object: this, propertyName: 'date2' }); | |
| } | |
| validateModel() { | |
| return this.controller.validate({ object: this, propertyName: 'date' }); | |
| } | |
| validateModel2() { | |
| return this.controller.validate({ object: this, propertyName: 'date2' }); | |
| } | |
| openErrorTarget(error, $event) { | |
| this[`${error.propertyName}Picker`].openDatePicker(); | |
| $event.preventDefault(); | |
| } | |
| } |
This file contains hidden or 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
| <!doctype html> | |
| <html> | |
| <head> | |
| <title>Aurelia</title> | |
| <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1"> | |
| </head> | |
| <body aurelia-app="main"> | |
| <h1>Loading...</h1> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.6/system.js"></script> | |
| <script src="https://rawgit.com/aurelia-ui-toolkits/aurelia-materialize-bundles/0.20.6/config2.js"></script> | |
| <script> | |
| System.import('aurelia-bootstrapper'); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or 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
| /******************************************************************************* | |
| * The following two lines enable async/await without using babel's | |
| * "runtime" transformer. Uncomment the lines if you intend to use async/await. | |
| * | |
| * More info here: https://github.com/jdanyow/aurelia-plunker/issues/2 | |
| */ | |
| //import regeneratorRuntime from 'babel-runtime/regenerator'; | |
| //window.regeneratorRuntime = regeneratorRuntime; | |
| /******************************************************************************/ | |
| import 'materialize'; | |
| export function configure(aurelia) { | |
| aurelia.use | |
| .standardConfiguration() | |
| .developmentLogging() | |
| .plugin('aurelia-materialize-bridge', bridge => bridge.useAll() ) | |
| .plugin('aurelia-validation'); | |
| aurelia.start().then(a => a.setRoot()); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment