FormRequest内的整数数组验证消息不被接受

时间:2020-06-24 10:21:20

标签: php laravel validation

我正在使用FormRequest进行验证,同时也覆盖消息功能来设置自定义消息。 不知何故,我无法使整数数组验证正常工作。这是我的位置:

这是我的请求的样子:

// ACCEPTED: 
{
    "name": "my name",
    "items": [20, 7, 1, 2]
}
// REJECTED
{
    "name": "Meine neue Kampagne",
    "items": ["ANY STRING HERE", 1, 2]
}

所以我希望我的商品只能是整数数组。这些是我的规则,完全有效:

return [
    'name' => 'required|max:191',
    'items' => 'array',
    'items.*' => 'int|exists:jobpostings,id'
]; 

到目前为止,我尝试了以下操作(所有操作均可在在线教程中找到):

return [
    'items.*.int' => "item is not int",
];

return [
    'items.*[
        'int' => "item is not int"
    ]
];

他们都给出了通用的“ items.0必须是整数”。消息。

有人可以帮我吗?

这是完整的FormRequest:

<?php

namespace App\Http\Requests\Campaign;

use Illuminate\Foundation\Http\FormRequest;

class CreateCampaignRequest 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
     */
    public function rules()
    {
        return [
            'name' => 'required|max:191',
            'items' => 'array',
            'items.*' => 'int|exists:items,id'
        ];
    }

    public function messages()
    {
        return [
            'name.required' => __('campaign.createCampaign.name_required'),
            'name.max' => __('campaign.createCampaign.name_max'),
            'items.array' => __('campaign.createCampaign.items_array'),
            'items.*.int' => __('campaign.createCampaign.items_*_int'),
            'items.*.exists' => __('campaign.createCampaign.items_*_exists'),
        ];
    }
}


0 个答案:

没有答案