-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DomCrawler] ChoiceFormField values for multiple and expanded options #40826
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
Comments
that's not how expanded choice fields are working. Each checkbox has its own index, submitting And the Form system of DomCrawler handles each checkbox on its own, not as a group named |
@stof thank you for your quick reply For testing the expanded multiple choice, I need to know the value position and submit it like this : public function testNewDocument()
{
// ... client
$client->submitForm('Save changes', [
'document[states][1]' => Document::REVIEWED,
'document[states][2]' => Document::PUBLISHED,
]);
// ... asserts
} But for non-extended multiple ChoiceType, I can still use it like this ? public function testNewDocument()
{
// ... client
$client->submitForm('Save changes', [
'document[states]' => [Document::REVIEWED, Document::PUBLISHED]
]);
// ... asserts
} Thank you for this explanation |
@jvancoillie yes, because a |
@stof thank you for all these details, I had not found my happiness in the document. :( |
I know this issue was closed long ago, but I modified #7337 (comment) to find and tick multiple boxes by multiple values, maybe someone finds it useful (FYI @jvancoillie): /**
* Find checkboxes which have any of the given value(s).
* Useful for multiple, expanded choice fields.
*
* Supply the field without a trailing '[]', e.g.
* `getMultipleCheckboxesByValues($form['my_form[someMultipleExpandedChoiceField]'], [25, 75]);`
*
* @param ChoiceFormField[]|FormField[] $field
*
* @return ChoiceFormField[]
*/
protected function getMultipleCheckboxesByValues(
array $field,
array|string $values,
bool $force = true
): array {
$values = array_map('strval', (array)$values);
$matchedFields = array_filter($field, function (ChoiceFormField $choice) use ($values) {
if ('checkbox' !== $choice->getType()) {
throw new \LogicException(sprintf('Field "%s" is not a checkbox (%s).', $choice->getName(), $choice->getType()));
}
return in_array($choice->availableOptionValues()[0], $values);
});
if ($force && empty($matchedFields)) {
throw new \InvalidArgumentException(sprintf('Field "%s" does not have any option with value "%s".', $field[0]->getName(), implode('", "', $values)));
}
return $matchedFields;
}
/**
* Ticks checkboxes which have any of the given value(s).
* Useful for multiple, expanded choice fields.
*
* Supply the field without a trailing '[]', e.g.
* `tickMultipleCheckboxesByValues($form['my_form[someMultipleExpandedChoiceField]'], [25, 75]);`
*
* @param ChoiceFormField[]|FormField[] $field
*/
protected function tickMultipleCheckboxesByValues(array $field, array|string $values, bool $force = true): void
{
foreach ($this->getMultipleCheckboxesByValues($field, $values, $force) as $checkbox) {
$checkbox->tick();
}
} I think it should be possible to do this out of the box, are you interested in a PR, @stof? |
Symfony version(s) affected: 5.2.4 (
symfony/dom-crawler
)Description
DomCrawler ChoiceFormField does not accept multiple values with multiple and expanded field options enabled
How to reproduce
WebTestCase submitForm with multiple values
FormType used in my test
phpunit throws me the following exception
Possible Solution
Detect if the inputs are multiple when extended is enabled during the initialization of ChoiceFormField#L215
The text was updated successfully, but these errors were encountered: