0

I'm trying to get an array of the custom error message in view for specific filed

controller

$rules = ['user_Name' => 'required|max:40',
              'image' => 'required',
              'image.*' => 'image|mimes:jpeg,png,jpg,svg,bmp|max:8000',
             ];

 // To get name of all image file and display in it in error message 
    if($request->hasfile('image')){  
      $validationArrayImage = array(); 
      $validationArraySize = array();
      $validationArrayType = array();;

      foreach($request->imageas $key => $file){           

        // check file extension for image type
        if($file->getClientOriginalExtension() != 'jpeg' &&
           $file->getClientOriginalExtension() != 'png' && 
           $file->getClientOriginalExtension() != 'jpg' &&
           $file->getClientOriginalExtension() != 'svg' &&
           $file->getClientOriginalExtension() != 'bmp'){


           $validationArrayImage['image.'.$key.'.image'] = 'The ' .  $file->getClientOriginalName() . ' not an image file.';
           $validationArrayType['image.'.$key.'.mimes'] = 'The ' .  $file->getClientOriginalName() . ' must be a file of type: jpeg, png, jpg, svg, bmp.';

        }

        // check file size for image size
        if($file->getSize() >= 8000000){

             $validationArraySize['image.'.$key.'.max'] = 'The ' .  $file->getClientOriginalName() . ' may not be greater than 8 Megabytes.';
           }

      }



      // check names array if it empty or not
      if(!empty($validationArrayType) || !empty($validationArraySize) || !empty($validationArrayImage)){

        // but all names in custom error, message 

       $messages = [
             $validationArrayImage,
             $validationArrayType,
             $validationArraySize,
              ];
       //dd($messages);
      }   

    } 



 if(!empty($messages)){
              $result = Validator::make($request->all(), $rules, $messages);
              /*foreach ($messages as $message) {
                dump($message);                     <--- Here I can see the array 
             }
            dd("stop");*/

           }
           else{ 
               $result = Validator::make($request->all(), $rules);
                //dd('$result');
           }

In the dump function, I can see all custom messages but why I get their empty array? enter image description here

I have try this but I got error exception Array to string conversion

               if(!empty($messages)){
              $result = Validator::make($request->all(), $rules, $messages);
              $errors = $result->errors();
              foreach ($errors->get('image.*') as $message) {
                dump($message);
            }
            dd("stop");
               //dd($result);
           }
           else{ 
               $result = Validator::make($request->all(), $rules);
                //dd('$result');
           }

In balde I have to try to get all message in the array but also I got error exception Array to string conversion View balde

                        <!-- To get error message for multi file -->
                        @if (!empty($messages)))
                        <h1>There is an error in your input array</h1>
                        <ul>
                            @foreach($messages as $errors)
                            @foreach($errors as $error)
                            <li>{{ $error }}</li>
                            @endforeach
                            @endforeach
                        </ul>
                        @else
                        <strong>Not array</strong>
                        @endif

Also, I have try this with the same error error exception Array to string conversion

<!-- To get error message for multi  -->
                            @if ($errors->has('Bank_Logo.*'))
                            <span class="invalid-feedback" style="display: block;" role="alert">
                                @foreach($errors->get('Bank_Logo.*') as $errors)
                                @foreach($errors as $error)
                                <strong>{{ $error }}</strong>
                                @endforeach
                                @endforeach
                            </span>
                            @endif

I'm trying to get all error messages with the error name for each file.

Any I idea how to get all error message array to view ?

Laravel version 5.8

1 Answer 1

1

With the custom rule defined, you might use it in your controller validation like so :

$validatedData = $request->validate([
       'f_name' => 'required|min:8',
       'l_name' => 'required',
   ],
   [
    'f_name.required'=> 'Your First Name is Required', // custom message
    'f_name.min'=> 'First Name Should be Minimum of 8 Character', // custom message
    'l_name.required'=> 'Your Last Name is Required' // custom message
   ]
);

Hope this helps...

Sign up to request clarification or add additional context in comments.

2 Comments

I need to this arrays to custom message in custom rule defined$validationArrayImage,$validationArrayType,$validationArraySize, but still, get this error Array to string conversion when I add a custom message like you do it works but I want to replace it with arrays and received in view, Also, I cannot use rule before if statement because in if statement it fills the array with a custom message
The problem was in $result = Validator::make($request->all(), $rules, $messages); the message argument should be an array not 2 dimensional array, So I change 3 array $validationArrayImage, $validationArrayType, $validationArraySize, to one array wtich is $message Thanks all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.