jojobudiman's avatar

ErrorException: Array to string conversion on validation image

So I created a validation error for the profile picture and cover with dimensions which you could see the code below that consists of arrays:

$rules = [
            'user_name' => 'min:2|max:255',
            'profilepic' => 'image|mimes:jpeg,png,jpg,gif,svg|dimensions:min_width=300,min_height=300,max_width=1500,max_height=1500,ratio=1/1',
            'profilecover' => 'image|mimes:jpeg,png,jpg,gif,svg|dimensions:min_width=1150, min_height=150,  max_width=1150, max_height=150'
];


$messages = [
            // 'user_name.required' => 'Name is required',
            'user_name.min' => 'Name must be at least 2 characters.',
            'user_name.max' => 'Name should not be greater than 255 characters.',
            // 'profilepic.required' => 'Profile picture is required',
            'profilepic.image' => 'Only accepting JPEG, PNG, BMP, GIF, SVG, or WEBP files',
            'profilepic.dimensions' =>
                ['min_width' => 'The :attribute dimension (width) cannot be less than :min_width px',
                 'max_width' => 'The :attribute dimension (width) cannot be more than :min_width px',
                 'min_height' => 'The :attribute dimension (height) cannot be less than :min_height px',
                 'max_height' => 'The :attribute dimension (height) cannot be more than :max_height px',
                 'ratio' => 'Your uploaded image ratio must be 1:1'
                 ],
            // 'profilecover.required' => 'Profile cover is required',
            'profilecover.image' => 'Only accepting JPEG, PNG, BMP, GIF, SVG, or WEBP files',
            'profilecover.dimensions' =>
                ['min_width' => 'The :attribute dimension (width) cannot be less than :min_width px',
                 'max_width' => 'The :attribute dimension (width) cannot be more than :min_width px',
                 'min_height' => 'The :attribute dimension (height) cannot be less than :min_height px',
                 'max_height' => 'The :attribute dimension (height) cannot be more than :max_height px'
                ]
];

$validator = Validator::make($request->all(), $rules, $messages);
if($validator->fails()) {
   return redirect()->action('UserController@editProfile')->withErrors($validator);
}

With the image element on the blade below in case there were errors:

<div class="form-group {{ $errors->has('profilepic') ? 'has-error' : '' }}">
    <label for="profilepic">{{__('Profile Picture')}}</label>
    <input type="file" class="form-control @error('profilepic') is-invalid @enderror" name="profilepic" id="profilepic">
</div>
@if ($errors->has('profilepic'))
    @foreach($errors->get('profilepic') as $error)
        <div class="invalid-feedback">
                <strong>{{ $error }}</strong>
        </div>
     @endforeach
@endif
<div class="form-group {{ $errors->has('profilecover') ? 'has-error' : '' }}">
    <label for="profilepic">{{__('Profile Cover')}}</label>
    <input type="file" class="form-control @error('profilecover') is-invalid @enderror" name="profilecover" id="profilecover">
</div>
@if ($errors->has('profilecover'))
    @foreach($errors->get('profilecover') as $error)
        <div class="invalid-feedback">
                <strong>{{ $error }}</strong>
        </div>
     @endforeach
@endif

After submitting the form with filled details, it shows Array to string conversion (View: /Applications/MAMP/htdocs/testproject/resources/views/backend/states/settings/profile-edit.blade.php) that is related to (also applies on profile cover) :

<div class="form-group {{ $errors->has('profilecover') ? 'has-error' : '' }}">
    <label for="profilepic">{{__('Profile Cover')}}</label>
    <input type="file" class="form-control @error('profilecover') is-invalid @enderror" name="profilecover" id="profilecover">
</div>

I wonder what's wrong with the validation errors since just getting started to Laravel. Thank you very much!

0 likes
1 reply
Nakov's avatar

@jojobudiman I don't see how is your profilecover an array as it is a single field. But is the error coming from this line maybe @error('profilecover') is-invalid @enderror

You are using $errors->has and @error I believe that both should do the same thing, so use one or the other. Try debugging if you remove this @error('profilecover') is-invalid @enderror from the input file and let's see if you get rid of the error, so you know where to look at.

Please or to participate in this conversation.