-
-
Save alexdresko/311699631a35c4a893b8c16d64324b38 to your computer and use it in GitHub Desktop.
Aurelia Validation Demo
This file contains 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> | |
<require from="./registration-form"></require> | |
<registration-form></registration-form> | |
</template> |
This file contains 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 App { | |
} |
This file contains 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 { | |
ValidationRenderer, | |
RenderInstruction, | |
ValidateResult | |
} from 'aurelia-validation'; | |
export class BootstrapFormRenderer { | |
render(instruction: RenderInstruction) { | |
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: Element, result: ValidateResult) { | |
if (result.valid) { | |
return; | |
} | |
const formGroup = element.closest('.form-group'); | |
if (!formGroup) { | |
return; | |
} | |
// add the has-error class to the enclosing form-group div | |
formGroup.classList.add('has-error'); | |
// add help-block | |
const message = document.createElement('span'); | |
message.className = 'help-block validation-message'; | |
message.textContent = result.message; | |
message.id = `validation-message-${result.id}`; | |
formGroup.appendChild(message); | |
} | |
remove(element: Element, result: ValidateResult) { | |
if (result.valid) { | |
return; | |
} | |
const formGroup = element.closest('.form-group'); | |
if (!formGroup) { | |
return; | |
} | |
// remove help-block | |
const message = formGroup.querySelector(`#validation-message-${result.id}`); | |
if (message) { | |
formGroup.removeChild(message); | |
// remove the has-error class from the enclosing form-group div | |
if (formGroup.querySelectorAll('.help-block.validation-message').length === 0) { | |
formGroup.classList.remove('has-error'); | |
} | |
} | |
} | |
} |
This file contains 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 class="form-group"> | |
<label class="control-label">${name}</label> | |
<input type="text" class="form-control" | |
model.bind="thing.text & validate"> | |
</div> | |
</template> |
This file contains 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} from 'aurelia-dependency-injection'; | |
import { bindable } from 'aurelia-templating'; | |
import { | |
ValidationControllerFactory, | |
ValidationController, | |
ValidationRules | |
} from 'aurelia-validation'; | |
import {BootstrapFormRenderer} from './bootstrap-form-renderer'; | |
@inject(ValidationControllerFactory) | |
export class ComplexControl { | |
@bindable thing; | |
controller = null; | |
@bindable name; | |
constructor(controllerFactory) { | |
this.controller = controllerFactory.createForCurrentScope(); | |
this.controller.addRenderer(new BootstrapFormRenderer()); | |
} | |
attached() { | |
ValidationRules | |
.ensure(a => a.text).displayName(this.name).satisfies(value => { | |
console.log('worrrrk'); | |
return true; | |
}).required() | |
.on(this.thing); | |
} | |
validate() { | |
this.controller.validate(); | |
console.log('should be validating'); | |
} | |
} | |
This file contains 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> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> | |
<style> | |
registration-form { | |
display: block; | |
max-width: 300px; | |
margin-left: auto; | |
margin-right: auto; | |
} | |
</style> | |
</head> | |
<body aurelia-app="main"> | |
<h1>Loading...</h1> | |
<script src="https://jdanyow.github.io/rjs-bundle/node_modules/requirejs/require.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/config.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/bundles/aurelia.js"></script> | |
<script src="https://jdanyow.github.io/rjs-bundle/bundles/babel.js"></script> | |
<script> | |
require(['aurelia-bootstrapper']); | |
</script> | |
</body> | |
</html> |
This file contains 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 function configure(aurelia) { | |
aurelia.use | |
.standardConfiguration() | |
.developmentLogging() | |
.plugin('aurelia-validation'); | |
aurelia.start().then(() => aurelia.setRoot()); | |
} |
This file contains 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> | |
<require from="./text-control"></require> | |
<require from="./complex-control"></require> | |
<form submit.delegate="submit()"> | |
<ul><li repeat.for="error of controller.errors">${error.message}</li></ul> | |
<div class="form-group"> | |
<label class="control-label" for="first">First Name</label> | |
<input type="text" class="form-control" id="first" placeholder="First Name" | |
value.bind="firstName & validate"> | |
</div> | |
<div class="form-group"> | |
<label class="control-label" for="last">Last Name</label> | |
<input type="text" class="form-control" id="last" placeholder="Last Name" | |
value.bind="lastName & validate"> | |
</div> | |
<text-control name="thing" view-model.ref="thingComponent" text.bind="thing.email"></text-control> | |
<text-control name="text1" view-model.ref="text1Component" text.bind="text1"></text-control> | |
<complex-control name="Thing2" view-model.ref="thing1Component" thing.bind="thing"></complex-control> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
</template> |
This file contains 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} from 'aurelia-dependency-injection'; | |
import { | |
ValidationControllerFactory, | |
ValidationController, | |
ValidationRules | |
} from 'aurelia-validation'; | |
import {BootstrapFormRenderer} from './bootstrap-form-renderer'; | |
@inject(ValidationControllerFactory) | |
export class RegistrationForm { | |
firstName; | |
lastName; | |
thing; | |
text1; | |
controller; | |
constructor(controllerFactory) { | |
this.controller = controllerFactory.createForCurrentScope(); | |
this.controller.addRenderer(new BootstrapFormRenderer()); | |
this.thing = {}; | |
} | |
bind() { | |
ValidationRules | |
.ensure(a => a.firstName).satisfies(value => { | |
console.log('firsty'); | |
return true; | |
}).required() | |
.on(this); | |
} | |
submit() { | |
this.controller.validate(); | |
this.text1Component.validate(); | |
this.thingComponent.validate(); | |
this.thing1Component.validate(); | |
console.log(this.thing1Component); | |
} | |
} | |
This file contains 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 class="form-group"> | |
<label class="control-label">${name}</label> | |
<input type="text" class="form-control" | |
value.bind="text & validate"> | |
</div> | |
</template> |
This file contains 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} from 'aurelia-dependency-injection'; | |
import { bindable } from 'aurelia-templating'; | |
import { | |
ValidationControllerFactory, | |
ValidationController, | |
ValidationRules | |
} from 'aurelia-validation'; | |
import {BootstrapFormRenderer} from './bootstrap-form-renderer'; | |
@inject(ValidationControllerFactory) | |
export class TextControl { | |
@bindable text; | |
controller = null; | |
@bindable name; | |
constructor(controllerFactory) { | |
this.controller = controllerFactory.createForCurrentScope(); | |
this.controller.addRenderer(new BootstrapFormRenderer()); | |
} | |
bind() { | |
ValidationRules | |
.ensure(a => a.text).displayName(this.name).satisfies(value => { | |
console.log('validatingggg'); | |
return true; | |
}).required() | |
.on(this); | |
} | |
validate() { | |
this.controller.validate(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment