Asked By: Anonymous
I’m trying to follow an aurelia (validation) tutorial. However, I know for a fact it needs to be updated. Even so, I thought that with the docs I would be able to figure out how to make it work, which is not happening.
The original code goes like this:
import {inject} from "aurelia-framework";
import {Validation} from "aurelia-validation";
@inject(Validation)
export class Edit {
constructor(validation) {
this.validation = validation.on(this)
.ensure("movie.title")
.isNotEmpty()
.ensure("movie.releaseYear")
.isNumber();
}
}
This doesn’t seem to work anymore. I get an injection error when trying to get validation into the constructor.
I went to the docs and there doesn’t seem to exist a Validation symbol anymore.
I tried injecting ValidationRules and Validator into my class but none support ensure or on methods.
I tried following the flow on the docs, where “on” would be the last method call. But since not even ensure is working, I got stucked.
Appreciate any help.
Solution
Answered By: Anonymous
Aurelia went through a lot of changes in the last two years, so there are quite a few tutorials that don’t work. But the official documentation on DocHub for Aurelia Validation should work for you.
Important note:
Aurelia Validation seems to have a limitation that it doesn’t work well with the sub-properties of an object, like you’re trying to do (this.movie.title
). Instead, you should do something like this:
import {ValidationRules} from "aurelia-validation";
export class Edit {
constructor() {
ValidationRules
.ensure("title").required()
.ensure("releaseYear").required()
.on(this.movie);
}
}
You can change .required()
to any of the valid rules. However, I don’t think that .isNotEmpty()
or .isNumber()
are on the list of valid rules. See the documentation.