html: Old value for select is not being set

I’m using larave 5.4 and laravelcollective 5.4, and when i create a select (multiple), if the data doesn’t pass the validation, the old selected values are not being set, i tracked the problem to the getSelectedValue method because it’s using the in_array in strict mode. The problem is that the data from the session is being retrieved as a string and the value being searched is an integer.

In this screeshot you can se that the first dump is an integer (it’s the $value parameter) and the second one is an array with a string inside (it’s the $selected parameter):

image

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 5
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Same issue here! I think, the strict parameter inside FormBuilder::getSelectedValue() is correct, because of “1” is different to 1. My opinion ist, that the real change should be in FormBuilder “getValueAttribute” on lines 1163-1170.

@see https://github.com/LaravelCollective/html/blob/5.4/src/FormBuilder.php#L1163-L1170

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

if (! is_null($value)) {
    return $value;
}

This is the place, where the given values are overwritten by request parameters. In this place, the better behaviour should look like.

if (! is_null($value)) {
    return $value;
}

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

My opinion is, that if i set an value to the select option, this option should have a higher priority than the request values. If i would set old values from request by my own, i would prefer array_map('intval', request('foobar', [])) to cast those values for my needs. I changed this workaround in vendor and this works for me. 😃

For your interest. If array keys set as string, for example “1”, PHP casts those to integers.

@david-ridgeonnet Yes, you are right.

Thumb up for changing strict to default false in FormBuilder::getSelectedValue(). 😃

Nevertheless, own given values should have a higher priority than the request. But this should be another issue. 😉

Hi guys. I’m facing similar issue here. I have {{ Form::select('topic_tags[]', $topic_tags, $agreement->topicTags->pluck('id')->all(), ['class' => 'form-control','multiple' => 'multiple']) }} inside a {!! Form::model($agreement, [ 'method' =>'PATCH', 'route' => ['frontend.ceap.agreements.update', $agreement->id]]) !!}

First time I open this view, $agreement->topicTags->pluck('id')->all() is able to correctly set the values for the select because it returns an array of int.

When a validation fails, what I realised is that old('topic_tags') returns an array of STRINGS and then the line return in_array($value, $selected, true) ? 'selected' : null; inside FormBuilder::getSelectedValue() is not able to correctly return the selected values because the third parameter asks the types of both values being tested to appear inside the array and the array items to be checked for equality.

I’ve being debugging validator and I realised that the request that is received by a controller already have the variable as a string. Thus, I think the best/only option is to set in_array to not be strict inside FormBuilder::getSelectedValue(). Can anyone confirm this?