我创建了一个可能return
有不同情况的自定义验证器类,所以我需要在每个这些之前设置自定义错误消息return
。
我创建了这个验证器类
class UserConfirmationMessageValidator extends Validator
{
public function canPassTheMessageConfirmation($attribute, $value, $parameters, $validator){
$confirmationKey = $validator->getData()["confirmationKey"];
$phoneNumber = $validator->getData()["phone"];
if(!isset($confirmationKey)){
if(case1){
// must set custom error message for this case
}else {
// must set custom error message for this case
}
return false;
}else {
if(case2){
return true;
}else{
// must set custom error message for this case
}
}
// can return with message that define in (AppServiceProvider => boot), if any cases
return false;
}
}
这是我的 AppServiceProvider 类
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// some other validators
Validator::extend('canPassTheMessageConfirmation', UserConfirmationMessageValidator::class."@canPassTheMessageConfirmation","you will receive sms. please wait");
}
}
设置规则的我的控制器
public function store(Request $request) // kullanıcı kaydetme
{
$validator = Validator::make($request->all(), [
'area1' => 'unique:vp_users|required|min:8|max:16|canPassTheMessageConfirmation',
'area2' => 'required|max:255',
'area2' => 'required|max:255',
'area4' => 'required|min:6|confirmed',
'area5' => 'required|date_format:Y-m-d',
'area6' => 'required|max:1',
]);
if ($validator->fails()) {
return response()->json($validator->errors()->first(),400); // Bad Request
} else {
$user = $this->create($request->all());
return response()->json($user, $request->statusCode);
}
}
验证器错误的转储输出,var_dump($validator->errors());
object(Illuminate\Support\MessageBag)[246]
protected 'messages' =>
array (size=1)
'area1' =>
array (size=1)
0 => string 'you will receive sms. please wait' (length=33)
protected 'format' => string ':message' (length=8)
当验证器因此canPassTheMessageConfirmation
规则失败时,validator
返回一条消息:“您将收到短信。请稍候”,该消息已在AppServiceProvider
boot
函数中定义。根据我的返回情况,我想在类的canPassTheMessageConfirmation
方法中更改此消息UserConfirmationMessageValidator
。如果我的解释不清楚。我可以根据您的意愿分享更多代码。
我想我找到了在扩展Validator
类中动态更改错误消息的方法并想分享。也许有人正在为这种定制而苦苦挣扎,请使用我的解决方案并感谢我。我注意到验证器对象有一个fallbackMessages
数组,其中包含在类的boot
方法上设置的自定义错误消息的参数AppServiceProvider
。您可以看到我如何在上述问题boot
的AppServiceProvider
类的方法上设置自定义错误消息。
验证器对象的示例输出
public 'fallbackMessages' =>
array (size=6)
'are_they_friend' => string 'you must be friend to complete this action!' (length=43)
'are_they_friend_with_all' => string 'all user you sent video must be your friend!' (length=44)
'is_member_of_group' => string 'you must be the member of this group!' (length=37)
'is_media_supported' => string 'this media type is not supported' (length=32)
'can_refresh_media_list' => string 'that is all for now' (length=19)
'can_pass_the_message_confirmation' => string 'you will receive sms. please wait' (length=33)
所以我得到fallbackMessages
并与我修改后的自定义消息合并。
$validator->setFallBackMessages(array_replace($validator->fallbackMessages,["can_pass_the_message_confirmation" => "My Message"]));
验证器错误的转储输出,var_dump($validator->errors());
object(Illuminate\Support\MessageBag)[246]
protected 'messages' =>
array (size=1)
'area1' =>
array (size=1)
0 => string 'My Message' (length=10)
protected 'format' => string ':message' (length=8)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句