Skip to content

Instantly share code, notes, and snippets.

@TurBas
Created May 11, 2017 18:49
Show Gist options
  • Save TurBas/11c8a315f23287fa239d79d8fada00fa to your computer and use it in GitHub Desktop.
Save TurBas/11c8a315f23287fa239d79d8fada00fa to your computer and use it in GitHub Desktop.
Aurelia disable button on validation errors
import {customAttribute, inject} from 'aurelia-framework'
import {ValidationController} from 'aurelia-validation'
import * as jQuery from "jquery";
@inject(Element)
@customAttribute('disabled-if-not-valid')
export class DisabledIfNotValid {
private element: JQuery;
private controller: ValidationController;
constructor(element: Element) {
this.element = jQuery(element);
this.element.prop('disabled', true);
}
valueChanged() {
let errorCount = this.controller.errors.length;
if (errorCount === 0) {
this.controller.validate();
}
this.element.prop('disabled', errorCount !== 0);
}
bind(bindingContext:{controller:ValidationController}) {
this.controller = bindingContext.controller;
}
}
<button class="btn btn-space btn-primary" disabled-if-not-valid="${controller.errors}" click.delegate="save()">Save</button>
@Jenselme
Copy link

This looks like a good reusable solution. However, I think I would have used disabled-if-not-valid.bind="controller.errors" instead of disabled-if-not-valid="${controller.errors}".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment