我为图像表单创建了一个验证规则。
在存储方法上它运行良好,但是我不希望在更新时要求填写图像字段,因为我可能只想更新标题。
class ImageRequest extends Request
{
/**
* Rules array
*/
protected $rules = [
'title' => 'required|string|between:3,60',
'alt' => 'sometimes|string|between:3,60',
'image' => 'required|image|max:4000|dimensions:min_width=200,min_height=200',
];
/**
* 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 $this->rules;
}
}
对于唯一性验证,我们可以添加自定义的查询条件:
'email' => Rule::unique('users')->ignore($user->id, 'user_id')
或者
'email' => Rule::unique('users')->where(function ($query) {
return $query->where('account_id', 1);
})
这是一种干净的方法来实现类似于required的功能吗?
仅对新图片应用required。