ReakyMark's avatar

Validator array data error htmlspecialchars() expects parameter 1 to be string

this is my HTML form

<tr>
    <td>
        <input type="text" name="title[]" value="" class="form-control" placeholder="Enter Title">
    </td>
    <td>
        <input type="file" class="btn-default btn" name="gallary[]" id="galary1"/>
    </td>
    <td>
        <i class="btn btn-danger fa fa-times-circle remove-gallary" aria-hidden="true"></i>
    </td>
</tr>

this is my Validator code

Validator::make(
    $req->all(),
    [
        'title.*' => 'required',
        'gallary.*' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
    ]
)->validate();

if i remove validator code it upload my image and create in DB but if i set Validate like above code i got error with htmlspecialchars() expects parameter 1 to be string.

how to solve it ?

0 likes
8 replies
morteza's avatar

How you show error message in blade?

Snapey's avatar

Its probably an error in your view, displaying the errors. This should be obvious from the error message (tells you what file/line in the code)

Is this only part of the form because you have title as an array?

ReakyMark's avatar

i have title and gallary(image upload) as array my multiple upload work fine except when i do validate.

this is full controller

 $title = $req->input('title');
if($req->hasFile('gallary'))
{
    $prodId = $req->input('product');
    $message = '';
                
    for($i = 0;$i < count($req->gallary); $i++)
    {
        $upload = $req->gallary[$i];
        $setNewFileName = AdminController::setNewFileName($upload, $title[$i]);
        $moveFileTo = $upload->move($path, $setNewFileName);
        if($moveFileTo)
        {
            $data       = [
                'gallary_image' => $setNewFileName,
                'gallary_title' => $title[$i],
                'prod_id'       => $prodId
            ];
            $originName= $upload->getClientOriginalName();
            if (Gallary::create($data)) {
                $message .= "<br>File name {$originName} is uploaded.";          
            }
        else
        {
             $message .= "<br>File name {$originName} not uploaded.";
                                    $fail = true;
        }
    }
   return redirect()->back()->with((isset($fail)) ? 'warning' : 'success', $message);

}

this is my message.blade.php

@if ($errors->any())  
    <?php
        $errorMessage = '';
        $i = 1;
        //var_dump($errors->all());
    ?>
    @foreach ($errors->all() as $error)
       <?php $errorMessage .= $i++.': '.$error.'<br>' ?>
    @endforeach
    {!! $createMessage->make($errorMessage, 'alert-danger') !!}
@endif

@if (Session::get('success') != '')  
    {!! $createMessage->make(Session::get('success'), 'alert-success') !!}
@endif

@if (Session::get('fail') != '')  
    {!! $createMessage->make(Session::get('fail'), 'alert-danger') !!}
@endif

@if (Session::get('warning') != '')  
    {!! $createMessage->make(Session::get('warning'), 'alert-warning') !!}
@endif

this is result of $req->all()

{
    "_token": "uc75zrtKXXAiCiCMHq8bCGeFhv8ZP2w830f3RGcU",
    "product": "126",
    "title": [
        "asd",
        "asdasd"
    ],
    "upload": null,
    "gallary": [
        {},
        {}
    ]
}

i'm not sure how laravel do upload if i need to get File name Size etc. i have to set that index like $req->upload[0] etc for multiple upload.

i don't think it relate to error coz i have remove message.blade.php from my layouts.app and try submit data still got error.can it be problem of how validator check on array value ?

Snapey's avatar

Why not say what line of code it fails on?

ReakyMark's avatar

@Snapey it only say htmlspecialchars() expects parameter 1 to be string etc.. that why i had no idea which line i got error.

ErrorException (E_ERROR)
htmlspecialchars() expects parameter 1 to be string, array given (View: C:\wamp64\www\laravel\resources\views\admin\products\add_edit.blade.php)
C:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Support\helpers.php
        }
 
        die(1);
    }
}
 
if (! function_exists('e')) {
    /**
     * Escape HTML special characters in a string.
     *
     * @param  \Illuminate\Contracts\Support\Htmlable|string  $value
     * @return string
     */
    function e($value)
    {
        if ($value instanceof Htmlable) {
            return $value->toHtml();
        }
 
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false); ||||||this line is red highlight
    }
}
 
if (! function_exists('ends_with')) {
    /**
     * Determine if a given string ends with a given substring.
     *
     * @param  string  $haystack
     * @param  string|array  $needles
     * @return bool
     */
    function ends_with($haystack, $needles)
    {
        return Str::endsWith($haystack, $needles);
    }
}
 
if (! function_exists('env')) {
    /**
     * Gets the value of an environment variable.
Arguments
"htmlspecialchars() expects parameter 1 to be string, array given (View: C:\wamp64\www\laravel\resources\views\admin\products\add_edit.blade.php)"

that all error

Snapey's avatar
Snapey
Best Answer
Level 122

so it tells you the fact the it is in your add_edit blade file

nothing to do with validation or the controller

its saying that somewhere you put {{ $something }} and that $something is an object not a string, so in the regard you are trying to echo an object

The error trace should tell you which line of the blade file it was on.

If you still cannot spot it then you will need to paste the whole add_edit.blade.php

1 like
ReakyMark's avatar

@Snapey Thank for help. i found issue with duplicate input name="" so {{ old('title') ?? $title }} cause error for sure. stuck 2 day now it solve Cheer :D

Please or to participate in this conversation.