在validate方法中,您有两个参数,一个用于字段输入,另一个用于验证消息。
我正在验证Laravel中的一个选择下拉列表,其中包含一组代理名称。我正在使用Rule::in((来执行此操作。验证工作正常,我收到标准错误消息"所选机构名称无效"。我想把它编辑成一个自定义的错误消息,但我很难像以前一样做到这一点。这是我的密码。
$agencies = Session::get('config.agency-names');
$agency_names = array();
for ($x = 0; $x < count($agencies['Agencies']); $x++) {
$name = $agencies['Agencies'][$x]["AgencyName"];
array_push($agency_names, $name);
array_push($agency_names, '');
}
$request->validate([
'referral' => 'required',
'agency-name' => ['required_if:referral,no', Rule::in($agency_names)],
'password' => 'required|min:6|regex:/[A-Z]/|regex:/[a-z]/|regex:/[0-9]/|confirmed'], [
// New custom agency name message
agency-name.Rule::in(agency_names) => 'NEW MESSAGE (DOESN'T WORK)',
// Custom password messages.
'password.confirmed' => 'Confirmation password did not match, please try again.',
'password.regex' => 'Password does not meet criteria, Please try again.',
'password.min' => 'Password does not meet criteria, please try again.',
]);
验证消息是根据具有验证类型的验证规则调用的,使用Rule对象不会改变这一点。
在你的代码中,你必须添加"机构名称.in",就像你使用in
验证一样
$request->validate([
'referral' => 'required',
'agency-name' => ['required_if:referral,no', Rule::in($agency_names)],
'password' => 'required|min:6|regex:/[A-Z]/|regex:/[a-z]/|regex:/[0-9]/|confirmed'], [
// New custom agency name message
'agency-name.in' => 'NEW MESSAGE ',
// Custom password messages.
'password.confirmed' => 'Confirmation password did not match, please try again.',
'password.regex' => 'Password does not meet criteria, Please try again.',
'password.min' => 'Password does not meet criteria, please try again.',
]);