-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Crawler] Selecting values of a ChoiceType field multiple and expanded #7337
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
This is actually a documentation problemas each checkbox is totally independent as you mentioned in your report. I've opened a PR to fix the docs. |
Ok, thats good but what I wanted to report is that it should be possible to select multiple and expanded choices by value and not by zero-based index of the options. This, in my opinion, would make tests much more readable (and writable). |
@mmenozzi +1. Same "problem" for me. I also tried to write: $form = $crawler->selectButton('Save')->form(array(
'ptc_noventobundle_supplier[name]' => 'Test Integration',
'ptc_noventobundle_supplier[categories]' => array(2, 1),
)); With this HTML structure:
But I get an InvalidArgumentException: Input "ptc_noventobundle_supplier[categories][]" cannot take "2" as a value (possible values: 4). |
As a workaround, it's possible to do: $values = $form->getPhpValues();
$values['ptc_noventobundle_supplier']['categories'] = array(2, 1);
$this->client->request($form->getMethod(), $form->getUri(), $values, $form->getPhpFiles()); |
@raziel057 that workaround wasn't working for me. My scenario is the same as above. I kept getting an error stating that I can only set the value to the first checkbox value (the one at index 0). Instead, I finally came up with a nice helper function which I include in my WebTestCase extended class (a custom child class). Example (sorry for the code formatting): protected function getCheckboxByValue($form, $field_name, $field_value)
{
return array_reduce($form->offsetGet($field_name), function ($carry, $field) use ($field_value) {
$options = array_map(function($option_value) {
return array(
'value' => $option_value
);
}, $field->availableOptionValues());
if ($field->containsOption($field_value, $options)) {
return $field;
}
});
} And call it like so: $field_name = 'ptc_noventobundle_supplier[categories]';
$field_value = '2';
$field = $this->getCheckboxByValue($form, $field_name, $field_value);
$field->tick(); |
It didn't work for me, I had to rewrite the function: /**
* Find checkbox
*
* @param \Symfony\Component\DomCrawler\Form $form
* @param string $name Field name without trailing '[]'
* @param string $value
*/
protected function findCheckbox($form, $name, $value)
{
foreach ($form->offsetGet($name) as $field) {
$available = $field->availableOptionValues();
if (strval($value) == reset($available)) {
return $field;
}
}
} |
On DomCrawler documentation is stated that is possible to select many options form a multiple select or checkboxes: http://d.pr/i/uY29.
I found that this is not true if the choice field is expanded (so checkboxes are generated). As stated in #5562 and in #5502, I also tried with:
But doesn't work. The problem is that $form['registration[interests]'] returns an array of ChoiceFormField (where every ChoiceFormField represent the single checkboxes of the options) so you can't call any method on it.
The only way to select values for a multiple and expanded choice field using DomCrawler is this:
Where 0 and 3 are zero-based indexes for the options symfony and cookies. This is uncomfortable because in tests we know the value of the choices and not the position in the markup of the correspondent checkbox.
The text was updated successfully, but these errors were encountered: