Last active
December 16, 2016 13:36
-
-
Save Thanood/237f82d064e5b2762f9344a8079b1837 to your computer and use it in GitHub Desktop.
Aurelia-Materialize bridge input fields aurelia-validation ordinary text field
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> | |
<div> | |
<input | |
value.bind="firstName & validate:rules" /> | |
<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> |
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 './MaterializeFormValidationRenderer'; | |
@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.length === 0) { | |
this.message = 'All is good!'; | |
} else { | |
this.message = 'You have errors!'; | |
} | |
}); | |
} | |
} |
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.5/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()); | |
} |
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
export class MaterializeFormValidationRenderer { | |
className = 'md-input-validation'; | |
classNameFirst = 'md-input-validation-first'; | |
render(instruction) { | |
for (let { result, elements } of instruction.unrender) { | |
for (let element of elements) { | |
this.remove(element, result); | |
} | |
} | |
for (let { result, elements } of instruction.render) { | |
for (let element of elements) { | |
this.add(element, result); | |
} | |
} | |
} | |
add(element, result) { | |
if (result.valid) { | |
return; | |
} | |
switch (element.tagName) { | |
case 'MD-INPUT': { | |
let label = element.querySelector('label'); | |
let input = element.querySelector('input') || element.querySelector('textarea'); | |
if (label) { | |
label.removeAttribute('data-error'); | |
} | |
if (input) { | |
input.classList.remove('valid'); | |
input.classList.add('invalid'); | |
result.target = input; | |
if (input.hasAttribute('data-show-errortext')) { | |
this.addMessage(element, result); | |
} | |
} | |
break; | |
} | |
case 'SELECT': { | |
const selectWrapper = element.closest('.select-wrapper'); | |
if (!selectWrapper) { | |
return; | |
} | |
let input = selectWrapper.querySelector('input'); | |
if (input) { | |
input.classList.remove('valid'); | |
input.classList.add('invalid'); | |
result.target = input; | |
if (!(input.hasAttribute('data-show-errortext') && | |
input.getAttribute('data-show-errortext') === 'false')) { | |
this.addMessage(selectWrapper, result); | |
} | |
} | |
break; | |
} | |
case 'INPUT' : { | |
if (element.hasAttribute('md-datepicker')) { | |
element.classList.remove('valid'); | |
element.classList.add('invalid'); | |
if (!(element.hasAttribute('data-show-errortext') && | |
element.getAttribute('data-show-errortext') === 'false')) { | |
this.addMessage(element.parentNode, result); | |
} | |
} else { | |
let input = element; | |
if (input) { | |
input.classList.remove('valid'); | |
input.classList.add('invalid'); | |
result.target = input; | |
if (input.hasAttribute('data-show-errortext')) { | |
this.addMessage(element, result); | |
} | |
} | |
} | |
break; | |
} | |
default: break; | |
} | |
} | |
remove(element, result) { | |
if (result.valid) { | |
return; | |
} | |
switch (element.tagName) { | |
case 'MD-INPUT': { | |
this.removeMessage(element, result); | |
let input = element.querySelector('input') || element.querySelector('textarea'); | |
if (input && element.querySelectorAll('.' + this.className).length === 0) { | |
input.classList.remove('invalid'); | |
input.classList.add('valid'); | |
} | |
break; | |
} | |
case 'SELECT': { | |
const selectWrapper = element.closest('.select-wrapper'); | |
if (!selectWrapper) { | |
return; | |
} | |
if ($(selectWrapper.parentElement).children().hasClass('md-input-validation') ) { | |
this.removeMessage(selectWrapper.parentElement, result); | |
} else { | |
this.removeMessage(selectWrapper, result); | |
} | |
let input = selectWrapper.querySelector('input'); | |
if (input && selectWrapper.querySelectorAll('.' + this.className).length === 0) { | |
input.classList.remove('invalid'); | |
input.classList.add('valid'); | |
} | |
break; | |
} | |
case 'INPUT' : { | |
if (element.hasAttribute('md-datepicker')) { | |
this.removeMessage(element.parentNode, result); | |
if (element && element.parentNode.querySelectorAll('.' + this.className).length === 0) { | |
element.classList.remove('invalid'); | |
element.classList.add('valid'); | |
} | |
} else { | |
let input = element; | |
if (input && element.querySelectorAll('.' + this.className).length === 0) { | |
input.classList.remove('invalid'); | |
input.classList.add('valid'); | |
} | |
} | |
break; | |
} | |
default: break; | |
} | |
} | |
addMessage(element, result) { | |
let message = document.createElement('div'); | |
message.id = `md-input-validation-${result.id}`; | |
message.textContent = result.message; | |
message.className = this.className; | |
if (element.querySelectorAll('.' + this.className).length === 0) { | |
message.className += ' ' + this.classNameFirst; | |
} | |
message.style.opacity = 0; | |
element.appendChild(message, element.nextSibling); | |
window.getComputedStyle(message).opacity; | |
message.style.opacity = 1; | |
} | |
removeMessage(element, result) { | |
let message = element.querySelector(`#md-input-validation-${result.id}`); | |
if (message) { | |
element.removeChild(message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment