Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0] Form field name as array results in error, when returned back to withInput #2718

Closed
tomsseisums opened this issue Nov 13, 2013 · 5 comments

Comments

@tomsseisums
Copy link

Huh, after some L4 updates, parts of my application have stopped working.

Route::get('keda', function()
{
    echo Form::open();

    echo Form::label('servers', 'Servers');
    echo Form::text('servers[]', Input::old('servers.0', 'zero'));

    echo '<br>';

    echo Form::submit('Sent');

    echo Form::close();
});

Route::post('keda', function()
{
    return Redirect::back()->withInput();
});

Always end up with:

'ErrorException' with message 'htmlentities() expects parameter 1 to be string, array given' in /srv/stone/vendor/laravel/framework/src/Illuminate/Support/helpers.php:508

Full stack trace

The problem appears to start in FormBuilder::getValueAttribute, where it looks up old input and favors it before explicit value*:

if ( ! is_null($this->old($name)))
{
    return $this->old($name);
}

That, runs FormBuilder::old that eventually calls FormBuilder::transformKey, that removes brackets, transforming server[] into server, and therefore, returing the array of values not a single value.

  • I feel that it's a bug. IMO, a passed (explicit) value should be always favored instead of old values.

What about this example:

Route::get('keda', function()
{
    echo Form::open();

    echo Form::label('servers', 'Servers');
    echo Form::text('server', !Input::old('server') ? 'troll' : 'ful' );

    echo '<br>';

    echo Form::submit('Sent');

    echo Form::close();
});

Route::post('keda', function()
{
    return Redirect::back()->withInput();
});

If there is no old entry, default to troll, if there is entry, use "ful", the problem, though, is that it always is filled with old entry never returning ful.


A simple reordering fixes both issues.


Is this a bug, intenional and I'm doing something wrong, or...?

@Milananas
Copy link

I also have this problem. After scrolling through the Laravel source a while I found out the same reason why this happens. Is this intended? If so, maybe arrays should be ignored? In my opinion though, as said above, if a specific value is given it should override the default behavior. Especially since there's already an Input::old() method that accepts a default value in case the old value isn't found.

@jackmcdade
Copy link

Having this problem too.

@rbruhn
Copy link

rbruhn commented Mar 19, 2014

Ditto. This issue was also mentioned in #2243
In my case, I'm adding a group of fields via ajax using a Add button. Each group consists of:
darwin_name[]
darwin_description[]
darwin_valid_response[]
darwin_inference[]
Apparently adding the key works. So I'm just going to try adding the key when adding a new group via ajax.

@anlutro
Copy link
Contributor

anlutro commented Mar 20, 2014

This problem has been present for as long as I can remember. It is hard to fix and the solution is to manually add an index to your input arrays - e.g. Form::text("servers[$i]")

@taylorotwell
Copy link
Member

I don't have any plans to continue developing the Form and HTML helpers myself, and am going to leave the rest of the development on those pieces up to the community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants