当前位置:  开发笔记 > 编程语言 > 正文

如何在Laravel中合并两个Request

如何解决《如何在Laravel中合并两个Request》经验,为你挑选了1个好方法。

在我的Laravel(5.1)项目中,我需要创建一个验证表单的请求.

但是对于这个请求,我想合并两个不同的请求:

第一个要求:

class AdvertisementRequest extends Request {

    public function authorize()
    {

        return true;
    }

    public function rules()
    {
        return [
            'ads_type' => 'required|numeric|in:0,1',
            'category' => 'required|numeric|exists:categories,id',
            'title' => 'required|alpha_num|max:45',
            'description' => 'required|alpha_num|max:2000',
            'price' => 'required|numeric',
        ];
    }

}

第二个要求:

class UserRegisterRequest extends Request {

    public function authorize()
    {

        return true;
    }

    public function rules()
    {
        $rules = [
            'form_type' => 'required|numeric|in:0,1',
            'user_type' => 'required|numeric|in:0,1',
            'phone' => 'required|phone_number',
            'region' => 'required|numeric|exists:regions,id',
            'department' => 'required|numeric|exists:departments,code',
            'postal_code' => 'required|postal_code',
            'city' => 'alpha|max:45',
            'id_city' => 'required|numeric|exists:cities,id',
            'last_name' => 'required|alpha_sp|max:45',
            'first_name' => 'required|alpha_sp|max:45',
            'pseudo' => 'required|alpha|max:45|unique:users',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|alpha_num|min:6|max:45',
        ];

        return $rules;
    }
}

我想创建第三个请求,将另外两个结合起来:

class UserAdvertisementRegisterRequest extends Request {

    public function authorize()
    {

        return true;
    }

    public function rules()
    {
        $rules = [
            'ads_type' => 'required|numeric|in:0,1',
            'category' => 'required|numeric|exists:categories,id',
            'title' => 'required|alpha_num|max:45',
            'description' => 'required|alpha_num|max:2000',
            'price' => 'required|numeric',

            'form_type' => 'required|numeric|in:0,1',
            'user_type' => 'required|numeric|in:0,1',
            'phone' => 'required|phone_number',
            'region' => 'required|numeric|exists:regions,id',
            'department' => 'required|numeric|exists:departments,code',
            'postal_code' => 'required|postal_code',
            'city' => 'alpha|max:45',
            'id_city' => 'required|numeric|exists:cities,id',
            'last_name' => 'required|alpha_sp|max:45',
            'first_name' => 'required|alpha_sp|max:45',
            'pseudo' => 'required|alpha|max:45|unique:users',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|alpha_num|min:6|max:45',
        ];

        return $rules;
    }
}

有没有重复我的代码可以解决这个问题?

对不起,我的英语不好 :/.

提前谢谢你的回复!



1> jedrzej.kury..:

最简单的方法是以下列方式在UserAdvertisementRegisterRequest :: rules方法中生成验证规则的合并列表:

class UserAdvertisementRegisterRequest extends Request { 
  public function rules()
  {
    return array_merge(
      with(new AdvertisementRequest)->rules(), 
      with(new UserRegisterRequest)->rules()
    );
  }
}

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有