Created
May 11, 2017 18:49
-
-
Save TurBas/11c8a315f23287fa239d79d8fada00fa to your computer and use it in GitHub Desktop.
Aurelia disable button on validation errors
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 {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; | |
} | |
} |
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
<button class="btn btn-space btn-primary" disabled-if-not-valid="${controller.errors}" click.delegate="save()">Save</button> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This looks like a good reusable solution. However, I think I would have used
disabled-if-not-valid.bind="controller.errors"
instead ofdisabled-if-not-valid="${controller.errors}"
.