1

这是我的验证规则:

public function rules() {

    $channel_id = Auth::user()->channels->first()->id;
    return [
        'name' => 'required|max:30|unique:channel,name,' . $channel_id,
        'slug' => 'required|max:30|alpha_num|unique:channel,slug,' . $channel_id,
        'description' => 'max:1000',
        'channelImage' => 'image',

    ];
}

public function messages() {
    return [

        'slug.unique' => 'That  unique URL has already been taken.',
        'channelImage.image' => 'Only jpeg, jpg, png, bmp, gif and svg formats are supported.',
    ];
}

虽然在填写表格时上传频道图片不是强制性的,但image验证规则的channelImage工作方式好像是强制性的,即我每次提交请求时都需要上传图片。

为什么会这样工作??我没有提到required规则,为什么当我不上传任何图像时channelImage它仍在验证?channelImage

4

1 回答 1

-1

尝试使用nullable

'channelImage' => 'nullable|image',
于 2017-08-15T21:54:35.910 回答