Forked from jdanyow/bootstrap-validation\bootstrap-form-validation-renderer.js
Created
July 15, 2016 05:03
-
-
Save Thanood/a2d27a7fadf7fbb0c09299d032977a40 to your computer and use it in GitHub Desktop.
Aurelia - bootstrap form validation feature
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} from 'aurelia-dependency-injection'; | |
import {validationRenderer} from 'aurelia-validation'; | |
@validationRenderer | |
@inject(Element) | |
export class BootstrapFormValidationRenderer { | |
constructor(boundaryElement) { | |
this.boundaryElement = boundaryElement; | |
} | |
render(error, target) { | |
if (!target || !(this.boundaryElement === target || this.boundaryElement.contains(target))) { | |
return; | |
} | |
// tag the element so we know we rendered into it. | |
target.errors = (target.errors || new Map()); | |
target.errors.set(error); | |
// add the has-error class to the bootstrap form-group div | |
const formGroup = target.querySelector('.form-group') || target.closest('.form-group'); | |
formGroup.classList.add('has-error'); | |
// add help-block | |
const message = document.createElement('span'); | |
message.classList.add('help-block'); | |
message.classList.add('validation-error'); | |
message.textContent = error.message; | |
message.error = error; | |
formGroup.appendChild(message); | |
} | |
unrender(error, target) { | |
if (!target || !target.errors || !target.errors.has(error)) { | |
return; | |
} | |
target.errors.delete(error); | |
// remove the has-error class on the bootstrap form-group div | |
const formGroup = target.querySelector('.form-group') || target.closest('.form-group'); | |
formGroup.classList.remove('has-error'); | |
// remove all messages related to the error. | |
let messages = formGroup.querySelectorAll('.validation-error'); | |
let i = messages.length; | |
while(i--) { | |
let message = messages[i]; | |
if (message.error !== error) { | |
continue; | |
} | |
message.error = null; | |
message.remove(); | |
} | |
} | |
} | |
// Polyfill for Element.closest and Element.matches | |
// https://github.com/jonathantneal/closest/ | |
(function (ELEMENT) { | |
ELEMENT.matches = ELEMENT.matches || ELEMENT.mozMatchesSelector || ELEMENT.msMatchesSelector || ELEMENT.oMatchesSelector || ELEMENT.webkitMatchesSelector; | |
ELEMENT.closest = ELEMENT.closest || function closest(selector) { | |
var element = this; | |
while (element) { | |
if (element.matches(selector)) { | |
break; | |
} | |
element = element.parentElement; | |
} | |
return element; | |
}; | |
}(Element.prototype)); |
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 {BootstrapFormValidationRenderer} from './bootstrap-form-validation-renderer'; | |
export function configure(config) { | |
config.container.registerHandler( | |
'bootstrap-form', | |
container => container.get(BootstrapFormValidationRenderer)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment