-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DomCrawler] Allow to add choices to single select #53559
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
base: 7.3
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
7.1 | ||
--- | ||
|
||
* Allow to add choices to single select | ||
|
||
7.0 | ||
--- | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,14 +143,14 @@ public function setValue(string|array|bool|null $value): void | |
*/ | ||
public function addChoice(\DOMElement $node): void | ||
{ | ||
if (!$this->multiple && 'radio' !== $this->type) { | ||
if (!$this->multiple && ('radio' !== $this->type && 'select' !== $this->type)) { | ||
throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name)); | ||
} | ||
|
||
$option = $this->buildOptionValue($node); | ||
$this->options[] = $option; | ||
|
||
if ($node->hasAttribute('checked')) { | ||
if ($node->hasAttribute('checked') || $node->hasAttribute('selected')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. type === "radio" a&& attribute checked || type === select && attribute selected ? |
||
$this->value = $option['value']; | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,18 @@ public function testSelects() | |
} catch (\InvalidArgumentException $e) { | ||
$this->assertTrue(true, '->setValue() throws an \InvalidArgumentException if the value is an array'); | ||
} | ||
|
||
$option = $this->createNode('option', 'Hello World', ['value' => 'hello_world']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a separate test instead of adding more things in the same test. This provides a much better experience:
Existing dom-crawler tests are not following the testing best practices because this is one of the oldest components of Symfony, at a time where the usage of phpunit in Symfony was still quite new. |
||
$field->addChoice($option); | ||
|
||
$this->assertNotSame('hello_world', $field->getValue()); | ||
$field->setValue('hello_world'); | ||
$this->assertSame('hello_world', $field->getValue(), '->setValue() changes the selected option to dynamically added one'); | ||
|
||
$option = $this->createNode('option', 'Mr. Robot', ['value' => 'mr_robot', 'selected' => true]); | ||
$field->addChoice($option); | ||
|
||
$this->assertSame('mr_robot', $field->getValue(), '->addChoice() changes the value to added choice if selected attribute is set'); | ||
} | ||
|
||
public function testSelectWithEmptyBooleanAttribute() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Message (and method docblock) should be updated.
@stof Would that be a BC break, as select were refused before ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smnandre given that's an
@internal
method BC break does not apply here