We can add validation rules to our models to make sure the data we store in our API is how we want it.
In this lesson we will add validation rules to the Product model.
We will make sure the product name has a minimal length using the validatesLengthOf
rule and that it is unique using the validatesUniquenessOf
rule.
For the price property we will add a custom validation to make sure that the value entered is not a negative integer. Additionally we will show how to do an async validation using validateAsync
. This can for instance be useful if you want the validation to depend on a value in the database or a remote system.