-
-
Save jawa-the-hutt/f32238bd50916cb53488976cc799c1e1 to your computer and use it in GitHub Desktop.
Aurelia Validation Demo - hidden field validate at save
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" for="name">Name</label> | |
<input type="text" class="form-control" id="name" placeholder="Name" | |
value.bind="user.name & validate"> | |
</div> | |
<div class="form-group"> | |
<label class="control-label" for="age">Age</label> | |
<input type="number" class="form-control" id="age" placeholder="Age" | |
value.bind="user.age & validate"> | |
</div> | |
<div if.bind="showAddress" class="form-group"> | |
<div class="input-group"> | |
<input type="text" value.bind="address & validate" class="form-control" name="address" id="address" placeholder="Address" /> | |
<span class="input-group-btn"> | |
<button type="button" class="btn btn-default" click.delegate="saveAddress()">Save Address</button> | |
</span> | |
</div> | |
</div> | |
<button type="button" class="btn btn-primary" click.delegate="showAddressButton()">Show Address</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, | |
validateTrigger, | |
Validator | |
} from 'aurelia-validation'; | |
import {BootstrapFormRenderer} from './bootstrap-form-renderer'; | |
@inject(ValidationControllerFactory, Validator) | |
export class App { | |
controller = null; | |
constructor(controllerFactory, validator) { | |
this.controller = controllerFactory.createForCurrentScope(); | |
this.controller.addRenderer(new BootstrapFormRenderer()); | |
this.controller.validateTrigger=validateTrigger.change; | |
this.validator = validator | |
this.user = { | |
name: '', | |
age: '' | |
} | |
this.showAddress = false; | |
this.address; | |
} | |
activate(){ | |
this.setupValidationRules(); | |
} | |
setupValidationRules() { | |
let rules = ValidationRules; | |
this.controller.addObject(this.user); | |
rules.customRule( | |
'integerBetween', | |
(value, obj, min, max) => value === null || value === undefined | |
|| value >= min && value <= max, | |
'\${$displayName} must be an integer between \${$config.min} and \${$config.max}.', | |
(min, max) => ({ min, max }) | |
); | |
rules = rules | |
.ensure('name').required().withMessage('Name is required.') | |
.ensure('age').satisfiesRule('integerBetween', 1, 12) | |
rules.on(this.user); | |
} | |
showAddressButton() { | |
this.showAddress = true; | |
} | |
saveAddress() { | |
let addressRule = ValidationRules | |
.ensure('address') | |
.required() | |
.on(this) | |
this.validator.validateProperty(this, addressRule) | |
.then(errors => { | |
if(errors.length = 0){ | |
this.showAddress = false; | |
} else { | |
console.log(errors); | |
} | |
}) | |
} | |
} | |
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, | |
ValidationError | |
} from 'aurelia-validation'; | |
export class BootstrapFormRenderer { | |
render(instruction) { | |
console.warn(instruction); | |
for (let { error, elements } of instruction.unrender) { | |
for (let element of elements) { | |
this.remove(element, error); | |
} | |
} | |
for (let { error, elements } of instruction.render) { | |
for (let element of elements) { | |
this.add(element, error); | |
} | |
} | |
} | |
add(element, error) { | |
const formGroup = element.closest('.form-group'); | |
if (!formGroup || formGroup.classList.contains('hide-error')) { | |
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 = error.message; | |
message.id = `validation-message-${error.id}`; | |
formGroup.appendChild(message); | |
} | |
remove(element, error) { | |
const formGroup = element.closest('.form-group'); | |
if (!formGroup) { | |
return; | |
} | |
// remove help-block | |
const message = formGroup.querySelector(`#validation-message-${error.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
<!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"> | |
</head> | |
<body aurelia-app="main" class="container"> | |
<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()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment