将Laravel验证与列无关的位置

发布于 2025-02-03 22:04:45 字数 409 浏览 4 评论 0原文

例如,我有一个HTTP请求来处理某些实体创建(让它成为角色) 验证看起来像这样:

class CreateRole extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'required|string|max:255',
            'permissions' => ['required', 'array'],
...

我有一些与名称或权限列无关的限制。 例如,可以创建的最大角色数量。

这种自定义验证可以放置在name属性上,但看起来错误。

ps:我知道自定义验证器。关于将其放置的问题...

For example, I have an HTTP request that handles some entity creation (let it be Role)
And validation looks like this:

class CreateRole extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'required|string|max:255',
            'permissions' => ['required', 'array'],
...

And I have some restrictions that are not related to either name or permission column.
For example the maximum amount of roles that can be created.

Such custom validation can be placed to the name property, but it looks wrong.

PS: I know about custom validators. Question about where to place it...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沦落红尘 2025-02-10 22:04:45

您可以在“ hook ”中使用 replect request createrole.php

class CreateRole extends FormRequest
{    
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'title'  => ['required', 'string', 'max:255'],
            'permissions' => ['required', 'array'],
        ];
    }
    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator  $validator
     * @return void
     */
    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            if (Role::count() > 10) {
                $validator->errors()->add('limit', 'Max number of Roles reached!');
            }
        });
    }
}

You can use after hook in the form request CreateRole.php

class CreateRole extends FormRequest
{    
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'title'  => ['required', 'string', 'max:255'],
            'permissions' => ['required', 'array'],
        ];
    }
    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator  $validator
     * @return void
     */
    public function withValidator($validator)
    {
        $validator->after(function ($validator) {
            if (Role::count() > 10) {
                $validator->errors()->add('limit', 'Max number of Roles reached!');
            }
        });
    }
}
听你说爱我 2025-02-10 22:04:45

您必须创建自定义验证类,如此 laravel文档并将其放入下面的效力类中。

 'name' => ['required', 'string', new CustomValidationClass],

You have to create custom validation class as shown in this Laravel document and place into you validatior class like below.

 'name' => ['required', 'string', new CustomValidationClass],
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文