# Applying Validation Rules to an Object of a Class

**URL:** <https://discourse.aurelia.io/t/applying-validation-rules-to-an-object-of-a-class/1072>\
**Category:** Uncategorized\
**Created:** [April 12, 2018, 11:26am UTC](https://discourse.aurelia.io/t/applying-validation-rules-to-an-object-of-a-class/1072 "2018-04-12T11:26:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Puppy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/puppy/32/734_2.png) [@Puppy](https://discourse.aurelia.io/u/Puppy)\
**Post date:** [April 12, 2018, 11:26am UTC](https://discourse.aurelia.io/t/applying-validation-rules-to-an-object-of-a-class/1072/1 "2018-04-12T11:26:08Z")

</div>

I don’t know how to use Applying Validation Rules to an Object of a Class.  
You can see it in below link at section ‘Applying Rules to an Object’.  
[http://aurelia.io/docs/plugins/validation#defining-rules](http://aurelia.io/docs/plugins/validation#defining-rules)  
As there is no html example .  
I tried it myself, but it does not work.

Please see below:

* * *

import { ValidationControllerFactory, Validator, ValidationRules, validateTrigger, ValidationController } from ‘aurelia-validation’;  
import { autoinject } from “aurelia-framework”;  
import { bindable, customElement } from “aurelia-templating”;

@autoinject  
export class EditTest {  
@bindable properties = { name: “”, id: “”};  
@bindable canSave: boolean = false;  
private validationController: ValidationController;

```
constructor(
    private validator: Validator,
    private controllerFactory: ValidationControllerFactory
) {
    this.validationController = controllerFactory.createForCurrentScope(validator);
    this.validationController.validateTrigger = validateTrigger.changeOrBlur;
    this.validationController.subscribe(event => this.validate());
}

attached() {

    ValidationRules.customRule(
        "validName",
        (value) => !/[*<>":?/|\\]/.test(value),
        "Name cannot contain following charactors: \\ / + * & % ？" 
    )

    ValidationRules
        .ensure('name').required()
        .satisfiesRule('validName')
        .on(this.properties);
}

private validate() {
    this.validator.validateObject(this)
        .then(results => {
            return this.canSave = results.every(result => {
                return result.valid;
            });
        });
}

```

}

## And html is here:

## template label\>edit model :\</label input value.bind=“properties.name & validate” button disabled.bind="!canSave"\>can save\</button template

Anyone can help out? With many thanks.

---

<div class="post-metadata">

**Author:** ![AlbertoDePena](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/albertodepena/32/224_2.png) [@AlbertoDePena](https://discourse.aurelia.io/u/AlbertoDePena)\
**Post date:** [April 12, 2018, 2:33pm UTC](https://discourse.aurelia.io/t/applying-validation-rules-to-an-object-of-a-class/1072/2 "2018-04-12T14:33:05Z")

</div>

@Puppy

This is how I do validation [change-password-dialog.ts](https://github.com/NBCoC/visitortrack/blob/master/src/VisitorTrack.Client/src/dialogs/change-password-dialog.ts) Also take a look at the FormValidator module. I manually trigger the validation. I m not aware of the subscriber on the controller itself. Hope it helps =)

---

<div class="post-metadata">

**Author:** ![Puppy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/puppy/32/734_2.png) [@Puppy](https://discourse.aurelia.io/u/Puppy)\
**Post date:** [April 13, 2018, 12:22am UTC](https://discourse.aurelia.io/t/applying-validation-rules-to-an-object-of-a-class/1072/3 "2018-04-13T00:22:16Z")

</div>

@AlbertoDePena  
I’ll take a look how it works.  
In my scenario, I need sync validation on input box, not when user click submit button.  
Anyway, thank you.

---

<div class="post-metadata">

**Author:** ![Puppy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/puppy/32/734_2.png) [@Puppy](https://discourse.aurelia.io/u/Puppy)\
**Post date:** [April 13, 2018, 1:05am UTC](https://discourse.aurelia.io/t/applying-validation-rules-to-an-object-of-a-class/1072/4 "2018-04-13T01:05:49Z")

</div>

I resolved this issue as I made a mistake. Change like below.

this.validator.validateObject(this) --------\> this.validator.validateObject(this.properties )
