Can*_*rin 2 php http-post laravel laravel-5 laravel-5.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',
];
}
}
Run Code Online (Sandbox Code Playgroud)
第二个要求:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想创建第三个请求,将另外两个结合起来:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有重复我的代码可以解决这个问题?
对不起,我的英语不好 :/.
提前谢谢你的回复!
最简单的方法是以下列方式在UserAdvertisementRegisterRequest :: rules方法中生成验证规则的合并列表:
class UserAdvertisementRegisterRequest extends Request {
public function rules()
{
return array_merge(
with(new AdvertisementRequest)->rules(),
with(new UserRegisterRequest)->rules()
);
}
}
Run Code Online (Sandbox Code Playgroud)