# Aurelia validation cannot read property \_\_rules\_\_

**URL:** <https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011>\
**Category:** Help Requests\
**Created:** [October 21, 2019, 7:19pm UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011 "2019-10-21T19:19:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidriduch](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/vidriduch/32/675_2.png) [@vidriduch](https://discourse.aurelia.io/u/vidriduch)\
**Post date:** [October 21, 2019, 7:19pm UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/1 "2019-10-21T19:19:27Z")

</div>

I’m trying to implement Aurelia validation, but I’m strugling to find any decent and recent example. The docs are good in describing how the validation work and should be setup, but its missing proper example.

“aurelia-validation”: “^1.5.0”,

I have a simple model (customer.model.ts):

```auto
import { ValidationRules } from 'aurelia-validation';

export interface ICustomer {
  name: string;
  email: string;
}

ValidationRules
  .ensure((x: ICustomer) => x.number).required()
  .ensure((x: ICustomer) => x.name).required()
  .ensure((x: ICustomer) => x.shortName).required()

```

in my view (customer-editor.ts):

```auto
...
import { ValidationController, validateTrigger } from 'aurelia-validation';

public customer: ICustomer = ({ name: 'One'} as ICustomer);

constructor(private validationCtrl: ValidationController) {
  this.validationCtrl.validateTrigger = validateTrigger.manual;
}

onSubmit() {
  this.validationCtrl.validate();
}

```

When I hit onSubmit, I get an error:

```auto
Possible Unhandled Promise Rejection: TypeError: Cannot read property ' __rules__' of undefined

```

I have tried (with no success) moving to rules to customer-editor.ts constructor and adding

```auto
.on(this);
// .on(this.customer);

```

Also using class instead of interface makes no difference.

Any ideas what am I doing wrong? Thank you!

P.S. I 've also asked this on StackOverflow, but no luck. ([https://stackoverflow.com/q/58450143/1861354](https://stackoverflow.com/q/58450143/1861354))

---

<div class="post-metadata">

**Author:** ![lancelot316](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/lancelot316/32/527_2.png) [@lancelot316](https://discourse.aurelia.io/u/lancelot316)\
**Post date:** [October 22, 2019, 12:42am UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/2 "2019-10-22T00:42:41Z")

</div>

This is just a guess but are you using `@inject` or `@autoinject` on the CustomerEditor class to inject the ValidationController in the constructor?  
[Validation Controller](https://aurelia.io/docs/plugins/validation#validation-controller)

---

<div class="post-metadata">

**Author:** ![vidriduch](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/vidriduch/32/675_2.png) [@vidriduch](https://discourse.aurelia.io/u/vidriduch)\
**Post date:** [October 22, 2019, 7:21am UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/3 "2019-10-22T07:21:47Z")

</div>

no, @autoinject is there …

---

<div class="post-metadata">

**Author:** ![MaximBalaganskiy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/maximbalaganskiy/32/831_2.png) [@MaximBalaganskiy](https://discourse.aurelia.io/u/MaximBalaganskiy)\
**Post date:** [October 22, 2019, 10:33am UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/4 "2019-10-22T10:33:58Z")

</div>

Check for duplicate aurelia-validation folders in node\_modules

---

<div class="post-metadata">

**Author:** ![vidriduch](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/vidriduch/32/675_2.png) [@vidriduch](https://discourse.aurelia.io/u/vidriduch)\
**Post date:** [October 23, 2019, 7:21am UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/5 "2019-10-23T07:21:46Z")

</div>

I have searched the node\_modules as well as re-installed all, but still the same problem …

---

<div class="post-metadata">

**Author:** ![MaximBalaganskiy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/maximbalaganskiy/32/831_2.png) [@MaximBalaganskiy](https://discourse.aurelia.io/u/MaximBalaganskiy)\
**Post date:** [October 23, 2019, 7:42am UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/6 "2019-10-23T07:42:06Z")

</div>

Also check for duplicate aurelia-templating and aurelia-binding… Run npm ddp

---

<div class="post-metadata">

**Author:** ![vidriduch](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/vidriduch/32/675_2.png) [@vidriduch](https://discourse.aurelia.io/u/vidriduch)\
**Post date:** [October 23, 2019, 10:07am UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/7 "2019-10-23T10:07:25Z")

</div>

Thank you, not sure if this worked, but after running npm ddp, I have converter interface to class and created an instance of model on page load, but still no luck. Then I have changed Validation rules to

```
.on(ICustomer)

```

and it suddenly worked.  
To simply use _.rules_ did not work for me neitehr using an iterface… This is how it look like now:

```auto
import { ValidationRules } from 'aurelia-validation';

export class Customer {
  name: string;
  email: string;
  short: string;
}

ValidationRules
  .ensure((x: Customer) => x.number).required()
  .ensure((x: Customer) => x.name).required()
  .ensure((x: Customer) => x.shortName).required()
  .on(Customer)
  // .rules this wont work ... not sure why

```

and controller

```auto
...
import { ValidationController, validateTrigger } from 'aurelia-validation';
...

public customer: Customer = new Customer()

constructor(private validationCtrl: ValidationController) {
  this.validationCtrl.validateTrigger = validateTrigger.manual;
}

onSubmit() {
  this.validationCtrl.validate();
}

```

---

<div class="post-metadata">

**Author:** ![MaximBalaganskiy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/maximbalaganskiy/32/831_2.png) [@MaximBalaganskiy](https://discourse.aurelia.io/u/MaximBalaganskiy)\
**Post date:** [October 24, 2019, 11:42pm UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/8 "2019-10-24T23:42:09Z")

</div>

You need to save `rules` to a variable and later add an object to the controller along with `rules`.  
If you don’t do that the `validate` binding behaviour has no idea what the rules are for a field.

See how it’s done here [https://aurelia.io/docs/plugins/validation#multiple-validation-controllers](https://aurelia.io/docs/plugins/validation#multiple-validation-controllers)

---

<div class="post-metadata">

**Author:** ![vidriduch](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/vidriduch/32/675_2.png) [@vidriduch](https://discourse.aurelia.io/u/vidriduch)\
**Post date:** [October 25, 2019, 7:21am UTC](https://discourse.aurelia.io/t/aurelia-validation-cannot-read-property-rules/3011/9 "2019-10-25T07:21:02Z")

</div>

thank you! I can see that someone has made documentation clearer on this point.
