Laravel 5.5 更新时有条件的表单请求验证规则

Laravel 5.5 conditional form request validation rule on update

我为图像表单创建了验证规则。

它在存储方法上工作正常,但我不希望在更新时需要图像字段,因为我可能只更新标题。

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;
    }
}

对于unique验证我们可以添加自定义查询条件:

'email' => Rule::unique('users')->ignore($user->id, 'user_id')

'email' => Rule::unique('users')->where(function ($query) {
    return $query->where('account_id', 1);
})

它是实现与 required 类似的东西的干净方法吗?

仅对新图像应用必需

您可以在规则中使用 switch 语句

 public function rules()
    {

        switch ($this->method()) {
            case 'GET':
            case 'DELETE': {
                return [];
            }
            case 'POST': {

                      return [
                          'first_name'=>'required',
                          'last_name'=>'required',
                        'email'=>'required|email|unique:users,email,'.$this->id,
                          'password'=>'',
                          'dob'=>'required',
                          'phone_one'=>'required',
                          'phone_two'=>'required',
                          //'user_role'=>'required',
                      //    'profile_image'=>'required'
                      ];
            }
            case 'PUT':
            case 'PATCH': {
                return [

                ];
            }
            default:break;
        }

你也可以使用条件,比如在更新时你有 id,所以基于它你可以检查它是更新还是插入,因为在插入时你没有 id 所以

创建另一个 class 来扩展请求 class,将其 DI 到您的更新控制器操作中

class UpdateImageRequest extends Request
{   
    /**
     * Rules array
     */
    protected $rules = [
        'title' => 'required|string|between:3,60',
        'alt'   => 'sometimes|string|between:3,60'
    ];

    /**
     * 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;
    }
}

更好的方法是在 Laravel 5.5 验证中使用 nullable

参考 Docs

The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.

class ImageRequest extends Request
{   
    /**
     * Rules array
     */
    protected $rules = [
        'title' => 'required|string|between:3,60',
        'alt'   => 'nullable|string|between:3,60',
        'image' => 'nullable|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;
    }
}

不过我最近使用了图像,它对我来说很有魅力。试试吧!

在这种情况下最简单的方法是另一种方法。默认情况下有更新规则,如果需要商店添加,如下所示:

class ImageRequest extends Request
{   
    /**
     * Rules array
     */
    protected $rules = [
        'title' => 'required|string|between:3,60',
        'alt'   => 'sometimes|string|between:3,60',
        'image' => '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()
    {
        $rules = $this->rules;

        if ($this->isMethod('POST')) {
           $rules['image'] = 'required|' . $rules['image']
        }

        return $rules;
    }
}

我找到了解决办法。

我将图片重命名为文件

路线是 homestead.app/images/1 on update and homestead.app/images store 所以 $image 属性 将是 $this->image = 1 更新$this->image = null 商店.

class ImageRequest extends Request
{
    /**
     * Rules array
     */
    protected $rules = [
        'title'=> 'required|string|between:3,60',
        'alt'  => 'sometimes|string|between:3,60',
        'file' => [
            '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()
    {
        $this->rules['file'][] = is_null($this->image) ? 'required' : 'sometimes';

        return $this->rules;
    }
}