Reputation: 3060
I'm using Laravel 5.1 and am struggling to resolve an problem handling old form data retrieved using 'Form::old()'
My form field names are arrays. When the form is submitted if there's a validation issue the user is redirected back as expected and I was then aiming to retrieve the values already submitted to re-populate the form. IN this case the values returned are in an array.
However the standard form helper is unable to handle arrays. It returns an error:
htmlentities() expects parameter 1 to be string, array given
So I decided to try and intercept the error and create my own helper function to capture the array, retrieve the correct value and then pass that to create the form element.
Here's my helper function:
public function arrayHidden($name, $value = null, $options = array())
{
if(isset($options) && array_key_exists('key', $options) && is_array($value)) {
$value = $value[$options['key']];
}
return $this->input('hidden', $name, $value, $options);
}
Here's how I'm using it in the page:
Form::arrayHidden('id[]', Form::old('id', $fixture->id), ['key'=>$key])
$key
is the index created in a foreach loop
Doing a var_dump of the returned $value
it's noted as a string however the error still occurs.
This is baffling - even though I've substituted an array qith the correct value the array still appears in the method.
Any ideas on how to avoid the array error?
Thank you
Upvotes: 0
Views: 272
Reputation: 10618
Form::arrayHidden('id[]', Form::old('id', $fixture->id), ['key'=>$key])
must be returning an array.
You must be using it in a view like this somewhere:
{{ Form::arrayHidden('id[]', Form::old('id', $fixture->id), ['key'=>$key]) }}
Everything inside the {{ }}
must be a string - hence the error.
Upvotes: 1