Farukest
Farukest

Reputation: 1518

How to change validation error message dynamically in extended (custom) validator class

I create a custom validator class that may return with diffrent cases, so i need to set custom error messages before each one these return.

I created this Validator Class

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;

    }
}

This is my AppServiceProvider Class

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // some other validators

        Validator::extend('canPassTheMessageConfirmation', UserConfirmationMessageValidator::class."@canPassTheMessageConfirmation","you will receive sms. please wait");

    }

}

My Controller where the rules set

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);
    }

}

Dumped output of validator's error, 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)

When validator fails with this canPassTheMessageConfirmation rule, validator returns a message : "you will receive sms. please wait" that was defined in AppServiceProvider boot function. I want to change this message in canPassTheMessageConfirmation method of UserConfirmationMessageValidator class, according to my return cases. If my explanation is not clear. I can share more code as your wish.

Upvotes: 0

Views: 749

Answers (1)

Farukest
Farukest

Reputation: 1518

I think i found the way to change the error message dynamically in extended Validator class and want to share. Maybe someone who is struggling with such customization use my solution and thanks me. I noticed that the validator object has a fallbackMessages array that has parameters of custom error messages that set on boot method of AppServiceProvider class. You can see how i set custom error message on boot method of AppServiceProvider class above question.

Example output of validator object

 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)

So i get the fallbackMessages and merge with my modified custom message.

$validator->setFallBackMessages(array_replace($validator->fallbackMessages,["can_pass_the_message_confirmation" => "My Message"]));

Dumped output of validator's error, 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)

Upvotes: 0

Related Questions