statamic / ideas

💡Discussions on ideas and feature requests for Statamic
https://statamic.dev
31 stars 1 forks source link

Custom Validation Rules #906

Closed stuartcusackie closed 1 year ago

stuartcusackie commented 1 year ago

I can't find any recent information on how to add a custom Laravel validation rule to a field.

You usually do this like so in a controller:

$request->validate([
    'field' => [new \App\Rules\CustomRule],
]);

but I don't see how we could do this in yaml and Validator::extend() seems to be abandoned in recent versions of Laravel. It would be great if we could do something like this:

validate:
  - required
  - \App\Rules\CustomRule

Any recommend strategies?

jasonvarga commented 1 year ago

Validator::extend() seems to be abandoned in recent versions of Laravel

What makes you say this? It still works fine. Use that.

jonassiewertsen commented 1 year ago

I did not read that the Validator is abandoned, but this is an example from the Statamic codebase:

https://github.com/statamic/cms/blob/3.3/src/Providers/ValidationServiceProvider.php

stuartcusackie commented 1 year ago

Yep, just realised this. Laravel have removed it from the documentation. Perhaps they are planning to remove it but it still works for now.

Thanks!