自定义错误消息不适用于自定义规则,请参阅变量$ messages uniqueTeamNameForcomp。
自定义规则很好,规则的自定义消息:必需也可以。
对于规则uniqueTeamNameForComp显示的错误消息是“unique_team_name_for_comp”,当它应该读取错误消息“此名称已存在于此竞赛中”。
控制器:
public function store(Request $request){
$rules = [
'name' => 'required|uniqueTeamNameForComp:'.$request->compzid,
'compz' => 'required'
];
$messages = array(
'uniqueTeamNameForComp' => 'This name already exist for this competition',
'required' => 'this works'
);
$this->validate($request,$rules,$messages);
}
服务提供者:
public function boot()
{
Validator::extend('uniqueTeamNameForComp', function ($attribute, $value, $parameters, $validator) {
$competitionId = $parameters[0];
return count(Tteam::where("comp_id", "=", $competitionId)->whereName($value)->get()) == 0;
});
}
答案 0 :(得分:0)
您从Laravel看到的输出实际上是在给您一个提示。它希望自定义消息由蛇案例中的验证规则键入,而不是驼峰案例。
$messages = array(
'unique_team_name_for_comp' => 'This name already exist for this competition',
'required' => 'this works'
);