-
-
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?
Conversation
fa4c796
to
a23a708
Compare
@@ -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 comment
The 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:
- we have a test name telling us what this is about
- the status of the test is separate, giving us better failures
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.
As this method is |
What would you suggest then? Can we remove |
@@ -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)); |
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
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 comment
The reason will be displayed to describe this comment to others. Learn more.
type === "radio" a&& attribute checked || type === select && attribute selected ?
Not sure why this was designed this way, but when using JS autocomplete's we cannot simulate it's behavior with a select element when a new choice was added to the list
cc @yceruto 🙏