I've got a view in my Laravel app (4.2) that is for editing a database record.
I'm storing two values - colour and heading - as arrays using the form::text method:
@foreach($colours as $key => $value)
{{ Form::label('heading_' . $key,'Heading ' . ($key + 1)) }}
{{ Form::text('heading[]', '', ['id' => 'heading_' . $key, 'class' => 'u-full-width']) }}
{{ Form::label('colour_' . $key,'Colour ' . ($key + 1)) }}
{{ Form::text('colour[]', $value, ['id' => 'colour_' . $key, 'class' => 'u-full-width']) }}
@endforeach
The initial edit form is fine but if I experience an issue (for example a field being empty) when I return to the original view (through our controller) it throws the following error:
htmlentities() expects parameter 1 to be string, array given
Bizarrely, it works fine for the colour[] fields but seems to be throwing an error for the heading[] fields.
Any thoughts on why this might be happening?
Cheers
Cole
You get the error htmlentities() expects parameter 1 to be a string, array given
because the []
appended to heading
and colour
make them an array in HTML and when POSTed
.
You're better off doing something like this (in PHP, in your controller, when you submit):
$colourArray = [];
foreach(Input::get('colours') as $colour) {
array_push($colourArray, htmlentities($colour));
}
Then you'll have stripped colours in the $colourArray
.
I haven't actually tested this, but I hope it makes sense.