Laravel 5.5在更新时使用条件表单请求验证规则

4

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

在存储方法上它运行良好,但是我不希望在更新时要求填写图像字段,因为我可能只想更新标题。

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


请接受任何解决您问题的答案以关闭此问题,并点赞任何/所有帮助解决您问题的答案。 - Mr. Pyramid
5个回答

2

你可以在规则内使用 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。

0
创建另一个继承 Request 类的类,将其 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;
    }
}

是的,这是备份解决方案(包括:'image' => 'sometimes|image|max:4000|dimensions:min_width=200,min_height=200'),但我希望能够重用相同的验证类,使用类似以下的内容:Rule::sometimes('required', function ($query) { return $query->where('image_id', 1); }); - Dorin Niscu
你可以从这个类继承普通类,只需更改规则即可。 - madalinivascu

0
更好的方法是在Laravel 5.5验证中使用nullable 参考文档

要验证的字段可能为空。这在验证原始类型(如字符串和整数)可能包含空值时特别有用。

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

然而,最近我使用了图片,它对我来说非常有效。你也可以试试!


0
在这种情况下,最简单的方法是另一种方式。默认情况下具有更新规则,如果是存储,则添加所需内容:
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;
    }
}

-3

我找到了解决方案。

我将image重命名为file

路径在update时为homestead.app/images/1,在store时为homestead.app/images,因此在update$image属性将是$this->image = 1,而在store$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;
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接