Replies: 1 comment
-
Depends on what you want to achieve. You could
<?php
// basic validation rules
return [
'id' => 'uuid',
'slug' => 'string',
]; app/Http/SomeFormRequest.php <?php
namespace App\Http;
class SomeFormRequest
{
public function rules(): array
{
return config('validation');
}
} or <?php
// basic validation rules
return [
'a' => [
'id' => 'uuid',
'slug' => 'string',
],
'b' => [
'id' => 'integer',
'label' => 'string',
],
]; app/Http/SomeFormRequest.php <?php
namespace App\Http;
class SomeFormRequest
{
public function rules(): array
{
return [
...config('validation.a'),
// more rules …
];
}
}
<?php
namespace App\Http;
class BaseFormRequest
{
public function rules(): array
{
return [
'id' => 'uuid',
'slug' => 'string',
];
}
} app/Http/SomeFormRequest.php <?php
namespace App\Http;
class SomeFormRequest extends BaseFormRequest
{
public function rules(): array
{
return [
...parent::rules(),
// more rules …
];
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have several groups of validation rules, messages and request preparation that I need to re-use in various request validators.
The solution I came up with was to write a new "rule set" class:
And then splatting those into my validators as needed:
This works fine, but it feels like I'm bending Laravel against its will slightly. Is there a sensible and Laravel-ish way of doing this?
Beta Was this translation helpful? Give feedback.
All reactions