-
-
Save JontyMC/aa6e3aacfe377249eba8aa7c1000a96d to your computer and use it in GitHub Desktop.
Aurelia Validation Demo - updateTrigger issue?
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> | |
<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="name">Name</label> | |
<input type="text" class="form-control" id="name" placeholder="Name" | |
value.bind="user.name & validate"> | |
</div> | |
<div class="form-group"> | |
<select class="form-control" value.bind="user.test & validate"> | |
<option model.bind="null">Select test</option> | |
<option>Test Option One</option> | |
<option>Test Option Two</option> | |
<option>Test Option Three</option> | |
</select> | |
</div> | |
<div class="form-group"> | |
<select class="form-control" value.bind="user.test2 & validate"> | |
<option model.bind="null">Select test 2</option> | |
<option>Test2 Option One</option> | |
<option>Test2 Option Two</option> | |
<option>Test2 Option Three</option> | |
</select> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
<ul> | |
<li repeat.for='e of controller.errors'> | |
${e.message} | |
</li> | |
</ul> | |
</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 | |
} from 'aurelia-validation'; | |
import {BootstrapFormRenderer} from './bootstrap-form-renderer'; | |
@inject(ValidationControllerFactory) | |
export class App { | |
user = {}; | |
controller = null; | |
constructor(controllerFactory) { | |
this.controller = controllerFactory.createForCurrentScope(); | |
this.controller.addRenderer(new BootstrapFormRenderer()); | |
ValidationRules | |
.ensure(x => x.name).required() | |
.ensure(x => x.test).required() | |
.ensure(x => x.test2).required() | |
.on(this.user); | |
} | |
attached() { | |
document.addEventListener('focusin', x => { | |
console.log('focusin', x); | |
}); | |
document.addEventListener('focusout', x => { | |
console.log('focusout', x); | |
}); | |
document.addEventListener('blur', x => { | |
console.log('blur', x); | |
}); | |
document.addEventListener('change', x => { | |
console.log('change', x); | |
}); | |
} | |
submit() { | |
this.controller.validate(); | |
} | |
} | |
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) { | |
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; | |
} | |
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 = result.message; | |
message.id = `validation-message-${result.id}`; | |
formGroup.appendChild(message); | |
} | |
remove(element, result) { | |
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 | |
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