Skip to content

Commit

Permalink
Fix setValue on checkbox and select fields if they are multiple (#526)
Browse files Browse the repository at this point in the history
Co-authored-by: Robert Freigang <[email protected]>
  • Loading branch information
robertfausk and Robert Freigang authored Jan 4, 2022
1 parent cddbb0b commit 23c920d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/DomCrawler/Field/ChoiceFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ public function setValue($value): void
return;
}

if ($this->selector->isMultiple()) {
$this->selector->deselectAll();
}
foreach ((array) $value as $v) {
$this->selector->selectByValue($v);
}
Expand Down
35 changes: 35 additions & 0 deletions tests/DomCrawler/Field/ChoiceFormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ public function testGetValueFromSelectMultipleIfOneIsSelected(callable $clientFa
$this->assertSame(['20'], $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
public function testSetValueFromSelectMultipleIfOneIsSelectedAfterAllHaveBeenSelectedBefore(callable $clientFactory): void
{
$crawler = $this->request($clientFactory, '/choice-form-field.html');
$form = $crawler->filter('form')->form();

$field = $form['select_multiple_selected_all'];
$this->assertInstanceOf(ChoiceFormField::class, $field);
$this->assertSame(['10', '20', '30'], $field->getValue());
$field->setValue('20');
$this->assertSame(['20'], $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
Expand Down Expand Up @@ -152,6 +167,26 @@ public function testGetValueFromCheckboxIfMultipleAreChecked(callable $clientFac
$this->assertSame(['checked_one', 'checked_two'], $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
public function testSetValueFromCheckboxIfOneIsCheckedAfterAllHaveBeenCheckedBefore(callable $clientFactory, string $type): void
{
$crawler = $this->request($clientFactory, '/choice-form-field.html');
$form = $crawler->filter('form')->form();

$field = $form['checkbox_multiple_checked'];

$this->assertInstanceOf(ChoiceFormField::class, $field);
// https://github.com/symfony/symfony/issues/26827
if (PantherClient::class !== $type) {
$this->markTestSkipped('The DomCrawler component doesn\'t support multiple fields with the same name');
}
$this->assertSame(['checked_one', 'checked_two'], $field->getValue());
$field->setValue('checked_two');
$this->assertSame('checked_two', $field->getValue());
}

/**
* @dataProvider clientFactoryProvider
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/choice-form-field.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
</select>
</label>

<label class="field">
select_multiple_selected_all
<select name="select_multiple_selected_all" multiple="multiple">
<option value="10" selected>ten</option>
<option value="20" selected>twenty</option>
<option value="30" selected>thirty</option>
</select>
</label>

<label class="field">
select_multiple_selected_multiple
Expand Down

0 comments on commit 23c920d

Please sign in to comment.