ideas icon indicating copy to clipboard operation
ideas copied to clipboard

Validation rule const and static method

Open SaeedDev94 opened this issue 3 years ago • 2 comments

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?

SaeedDev94 avatar May 18 '21 12:05 SaeedDev94

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.

tpetry avatar May 21 '21 07:05 tpetry

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)

SaeedDev94 avatar May 21 '21 08:05 SaeedDev94