Skip to main content
It is possible to add extra rules as attributes to properties of a data object:
These rules will be merged together with the rules that are inferred from the data object. So it is not required to add the required and string rule, these will be added automatically. The rules for the above data object will look like this:
For each Laravel validation rule we’ve got a matching validation attribute, you can find a list of them here.

Referencing route parameters

Sometimes you need a value within your validation attribute which is a route parameter. Like the example below where the id should be unique ignoring the current id:
If the parameter is a model and another property should be used, then you can do the following:

Referencing the current authenticated user

If you need to reference the current authenticated user in your validation attributes, you can use the AuthenticatedUserReference:
When you need to reference a specific property of the authenticated user, you can do so like this:
Using a different guard than the default one can be done by passing the guard name to the constructor:

Referencing container dependencies

If you need to reference a container dependency in your validation attributes, you can use the ContainerReference:
It might be more useful to use a property of the container dependency, which can be done like this:
When your dependency requires specific parameters, you can pass them along:

Referencing other fields

It is possible to reference other fields in validation attributes:
These references are always relative to the current data object. So when being nested like this:
The generated rules will look like this:
If you want to reference fields starting from the root data object you can do the following:
The rules will now look like this:

Rule attribute

One special attribute is the Rule attribute. With it, you can write rules just like you would when creating a custom Laravel request:

Creating your validation attribute

It is possible to create your own validation attribute by extending the CustomValidationAttribute class, this class has a getRules method that returns the rules that should be applied to the property.
You can only use these rules as an attribute, not as a class rule within the static rules method of the data class.