framework: Multiple select with [] in name can't be crawled

Hello,

If you have a select, let’s say it looks like

<select name="test[]" multiple>
    <optgroup value="Testing">
        <option value="1">Testing 1</option>
        <option value="2">Testing 2</option>
    </optgroup>
</select>

And if you try to use

$this->select([1,2], 'test[]');

It won’t work, while it works in the Laravel side as soon as it goes to dom-crawler and enters the get function in FormFieldRegistry https://github.com/symfony/dom-crawler/blob/6bf50c092a5fcfb700e53addabb82ebd77625b27/FormFieldRegistry.php#L82 it breaks. The form field registry stores selects, regardless if they have a [] or not without it. So in this case if I changed it to

$this->select([1,2], 'test');

It would work on the dom-crawler, but it won’t work for the Laravel CrawlerTrait. Basically if you have

$this->select([1,2], 'test[]');

In your test, and then add something like

if ($name == 'test[]') $name = 'test';

On https://github.com/symfony/dom-crawler/blob/6bf50c092a5fcfb700e53addabb82ebd77625b27/FormFieldRegistry.php#L84

It works.

So before the field names are sent to dom-crawler they need to have [] stripped.

dom-crawler version - v2.7.5 laravel foundation version - v5.1.19

About this issue

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

Most upvoted comments

In case anyone else finds this and is struggling to write an integration test for multiselect, I managed to get it to work by doing what @spikerok said.

Taking the method @spikerok linked to : https://github.com/laravel/framework/blob/5.2/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php#L845

And adding in the line @spikerok said to add in: $element = str_replace("[]", "", $element);

Gives you this:

protected function storeInput($element, $text)
    {
        $this->assertFilterProducesResults($element);
        $element = str_replace('#', '', $element);
        $element = str_replace("[]", "", $element);
        $this->inputs[$element] = $text;
        return $this;
    }

Simply copy and paste that into your test case class and you should be able to run your tests just fine.

Just incase you were wondering, my test looks like this:

$this->actingAs($adminUser)
             ->visit('/admin/project/create')
             ->type('James', 'name')
             ->select([1, 2], 'users[]')

I am still on L5.1… however, update of https://github.com/laravel/framework/blob/5.2/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php#L845

with addition of: $element = str_replace("[]", "", $element);

Would resolve the problem.

@GrahamCampbell how would you go about testing an input/select that sends and array to the request?