Skip to content

Instantly share code, notes, and snippets.

@Thanood
Last active November 29, 2017 18:51
Show Gist options
  • Save Thanood/59525172dc31afb0de610d0b174676a0 to your computer and use it in GitHub Desktop.
Save Thanood/59525172dc31afb0de610d0b174676a0 to your computer and use it in GitHub Desktop.
Aurelia-Materialize bridge input fields aurelia-validation
md-colors

Currently, the styles for validation messages are hosted in another component Please use <md-colors></md-colors> in your main view to use colors.

This will be refactored.

<template>
<require from="./blurOnEnter"></require>
<md-colors></md-colors>
<div>
<md-input
md-blur-on-enter="true"
md-label="First Name"
md-value.bind="firstName & validate:rules"
>
</md-input>
<md-input
md-blur-on-enter="true"
md-label="Last Name"
md-placeholder="Last Name"
md-validate="true"
md-validate-success="good"
md-value.bind="lastName & validate:rules">
</md-input>
<md-input
md-blur-on-enter="true"
md-label="Email"
md-validate="true"
md-validate-success="yes!!"
md-value.bind="email & validate:rules">
</md-input>
<md-input
md-blur-on-enter="true"
md-label="Required without error text"
md-show-errortext="false"
md-value.bind="noErrorText & validate:rules">
</md-input>
<md-input
md-blur-on-enter="true"
md-label="put some text here"
md-value.bind="textareaValue & validate:rules"
md-text-area="true">
</md-input>
</div>
<div style="margin-top: 15px;">
<button md-button click.delegate="validateModel()">validate</button>
</div>
<h5>${message}</h5>
<ul style="margin-top: 15px;" show.bind="controller.errors.length">
<li repeat.for="error of controller.errors">
<a href="#" click.delegate="error.target.focus()">
${error.message}
</a>
</li>
</ul>
</template>
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 {
message = '';
firstName = '';
lastName = 'Doe';
email = '';
noErrorText = '';
textareaValue = '';
controller = null;
rules = ValidationRules
.ensure('firstName')
.required()
.ensure('lastName')
.minLength(4)
.ensure('email')
.required()
.withMessage('We need your email')
.email()
.ensure('noErrorText')
.required()
.ensure('textareaValue').displayName('Some text')
.required()
.rules;
constructor(controller: ValidationController) {
this.controller = controller;
this.controller.addRenderer(new MaterializeFormValidationRenderer());
}
validateModel() {
this.controller.validate().then(v => {
if (v.valid) {
this.message = 'All is good!';
} else {
this.message = 'You have errors!';
}
});
}
onFocus() {
console.log('focus');
}
}
import {customAttribute, inject} from 'aurelia-framework';
@customAttribute('blur-on-enter')
@inject(Element)
export class BlurOnEnter {
constructor(element) {
this.element = element;
this.handleKeyUp = this.handleKeyUp.bind(this);
}
attached() {
this.element.addEventListener('keyup', this.handleKeyUp);
}
detached() {
this.element.removeEventListener('keyup', this.handleKeyUp);
}
handleKeyUp(e) {
if (e.keyCode === 13) {
this.element.blur();
}
}
}
<!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.31.0/config2.js"></script>
<script>
System.import('aurelia-bootstrapper');
System.import('materialize/dist/css/materialize.css!');
</script>
</body>
</html>
/*******************************************************************************
* 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