在验证器(Laravel)内添加多个变量

问题描述 投票:1回答:1

目前,我在Laravel项目中有这个(有效的)验证器

$validator = Validator::make(
  $insert_data1,
  [
      '*.emp_id' => "required|exists:users,company_id",
  ],
  [
      '*.emp_id.exists' => 'The selected :attribute is invalid. Employee does not exists!',
  ]
);

 if($validator->fails()){
    return redirect()
    ->back()
    ->with(['errors'=>$validator->errors()->all()])
    ->with('modal',$modal);
  }

现在我需要为像这样的其他变量添加另一个验证

     $insert_data2,
     [
       '*.emp_id' => "required|exists:users,company_id",
     ],
     [
        '*.emp_id.exists' => 'The selected :attribute is invalid. Stakeholder does not exists!',
     ]

如何添加$ insert_data2inside of thevalidator`?

php laravel eloquent
1个回答
0
投票

您需要做这样的事情

$validation = array(
    'name' => 'required|max:60',
    'email' => 'required|email',
    // N Number of validation Rules
);

$messages = array(
    'name.required' => 'A Name is required',
    'name.max' => 'Name can only be of max 60 alphanumerics',
    'email.required' => 'Email address is required',
    'email.email' =>'Email must be a valid email'
    // N number of validation messages
);

$response = Validator::make($request,$validation,$messages);

if($response->fails()){
    //Your Logic
}
© www.soinside.com 2019 - 2024. All rights reserved.