Now my problem is, that I have no clue how to write the ValidationRenderer to set a class “.is-invalid” to the respective input field (one of the two).
With <date-range-picker value.bind="value & validateOnChange") I only get the anchor for the total template including both input fields. Calling element.previousElementSibling I only can access the last input field of the both.
Following my try that works for all input fields but my custom element date-range-picker:
add(element: Element, result: ValidateResult) {
console.log($(element));
toastr.options = this.toastrOptions;
if (!result.valid) {
if (element.nodeType === 8) element = element.previousElementSibling; //Validation for custom elements like datepicker
toastr.error(result.message);
element.classList.add('is-invalid');
}
}
Having validation renderer decide how to display errors is separating validation from an element, which I think is a mistake.
What I’ve done for the aurelia materialize bridge is a custom validation renderer which looks for certain methods added to the custom elements. If those methods exist they are getting called. Since the methods “belong” to the element’s view model they are in full control of the DOM and element’s state.
and one of the elements
Look at the mdRenderValidateResults and mdUnrenderValidateResults methods.
This approach works especially well with custom elements. There is still some default behaviour for a standard input element.