This is the documentation for
v3.
You can switch versions in the menu on the left/at the top.
Check your current version with the following command:
composer show spatie/laravel-data
Rule inferrers will try to infer validation rules for properties within a data object.
A rule inferrer can be created by implementing the RuleInferrer
interface:
interface RuleInferrer
{
public function handle(DataProperty $property, RulesCollection $rules): array;
}
A collection of previous inferred rules is given, and a DataProperty
object which represents the property for which the value is transformed. You can read more about the internal structures of the package here.
The RulesCollection
contains all the rules for the property represented as ValidationRule
objects.
You can add new rules to it:
$rules->add(new Min(42));
When adding a rule of the same kind, a previous version of the rule will be removed:
$rules->add(new Min(42));
$rules->add(new Min(314));
$rules->all(); // [new Min(314)]
Adding a string rule can be done as such:
$rules->add(new Rule('min:42'));
You can check if the collection contains a type of rule:
$rules->hasType(Min::class);
Or remove certain types of rules:
$rules->removeType(Min::class);
In the end, a rule inferrer should always return a RulesCollection
.
Rule inferrers need to be manually defined within the data.php
config file.