ideas
ideas copied to clipboard
Validation rule const and static method
Hi,
It would be better if we create some const in Illuminate\Validation\Rule
class to suggest rules in IDE
Dev must write the exact rule name or install plugin to suggest rules, i think this way is more dev friendly:
class Rule {
/**
* The field under validation must be present in the input data and not empty
*/
public const required = 'required';
}
Then in controller:
$request->validate([
'field' => [Rule::required]
]);
By typing Rule::
dev can see all available validation rules with description
What do you think?
This will get really messy with options for rules:
$request->validate([
'field' => [Rule::required, Rule::max.':255', Rule::email.':rfc,dns']
]);
If you are using PHPStorm the Laravel Idea fully supports validations strings with auto-suggest.
For rules with arguments we can use static methods
Like: Rule::unique($table, $column = 'NULL')
that already exists
Laravel idea that you mentioned does not suggest in all cases, also it's a paid plugin
$request->validate([
'field' => [Rule::required, Rule::max(255), Rule::email(['rfc', 'dns'])]
]);
You can see this pattern in angular ReactiveFormsModule:
age = new FormControl(10, [Validators.required, Validators.min(18)]);
(For example Rule::min($value)
can simply generate validation string: min:value
)