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
ChoiceType returns wrong data on submit if clearMissing is false #16802
Comments
@webmozart can you take a look ? |
@ossinkine the |
@1ed My bad. |
@stof @webmozart Just upgraded in 2.8 and I'm facing the same issue. Seems like it comes from the expanded option being set to true, it's as if the data wasn't properly cleaned in the ChoiceType class. The following code fixes the problem, but I'm really not sure if that's the way to fix it. <?php
// Symfony\Component\Form\Extension\Core\Type\ChoiceType.php
foreach ($form as $child) {
$value = $child->getConfig()->getOption('value');
// Add the value to $data with the child's name as key
if (isset($valueMap[$value])) {
$data[$child->getName()] = $value;
unset($unknownValues[$value]);
continue;
}
// Doing this fixes the problem
$data[$child->getName()] = false;
} |
Hi @ossinkine, I can confirm your issue, a workaround it to call the field data : $form->get('choice')->getData()
// will get the expected
string 'bar' (length=3) It's a problem of propagation of submitted data. I'll send a PR as a proposal to fix this issue, thanks for reporting it. Status: reviewed |
Hi @ossinkine, could you please try the fix provided in #17771 to see if it works for your issue ? |
Hi @HeahDude |
Thanks for the feedback. |
So as #17771 is closed without a pull (for good reasons, I suppose) this problem still exists in all current Symfony versions. |
This seems to have been fixed in the meantime as I can no longer reproduce. |
The issue is still reproduced. |
you might want link this url to one remaining linked issue that is still open |
I did fall into this issue. So currently the ChoiceType is given clearMissing = false here: symfony/src/Symfony/Component/Form/Form.php Line 578 in dbe37de
Maybe we should introduce a new form configuration e.g.: |
Would #35938 help here? |
@xabbuh did test it but sadly that does not solve this issue. |
@xabbuh oh you asked if it helps. Basically we could go on that flag and do: if ($child->getConfig()->getAttribute('_false_is_empty')) {
$clearMissing = true;
}
$child->submit($isSubmitted ? $submittedData[$name] : null, $clearMissing); at symfony/src/Symfony/Component/Form/Form.php Line 578 in dbe37de
and then the entity looks good for me. But not sure if its a good idea to go on that flag and how it will be solved in Symfony 5.1. /cc @fancyweb @HeahDude |
We have run into this issue today as well on symfony 5.1.3. We found that a form eventlistener helped us work around the issue: <?php
namespace App\Form\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Event\PreSubmitEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class ChoiceSubscriber implements EventSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SUBMIT => 'onPreSubmit',
];
}
/**
* @param PreSubmitEvent $event
* @throws \Symfony\Component\Form\Exception\OutOfBoundsException
* @throws \Symfony\Component\Form\Exception\RuntimeException
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function onPreSubmit(PreSubmitEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
if (isset($data['choice']) && $data['choice'] !== $form->get('choice')->getData()) {
$form->get('choice')->setData(null);
}
}
} The work around constitutes checking during the pre submit event if the submitted data has changed compared to the pre-populated form data. If this is the case reset the current form data to null. Hopefully this helps others who might run into this problem in the future until a patch is made to fix it. |
Hey, thanks for your report! |
. |
I am not sure I understand the issue. Submitting invalid values to a choice type means that a |
Indeed, I think this can be closed after all. Problem in post of OP was that he is submitting choice key instead of value. |
I'll update my reproducer to the latest Symfony 4.4 and try to reproduce. |
I've updated Symfony in my reproducer to the latest 4.4 version and the issue is still reproducer. |
No, |
@ostrolucky please look at reproducer https://github.com/ossinkine/symfony-issue-16802/blob/771f6579ad4ba7c27b1d9bf44d4db0c8092588c2/src/Form/TestType.php#L17 $builder
->add('text', TextType::class)
->add('choice', ChoiceType::class, ['choices' => ['FOO' => 'foo', 'BAR' => 'bar'], 'expanded' => true])
; choices are values, so |
Thanks, I can now reproduce it. Status: Reviewed |
Could you please also update original post? |
@ostrolucky updated |
Can you try out #40417 please? |
…ssing is set to false (xabbuh) This PR was merged into the 4.4 branch. Discussion ---------- [Form] clear unchecked choice radio boxes even if clear missing is set to false | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #16802 | License | MIT | Doc PR | Commits ------- e7b4851 clear unchecked choice radio boxes even if clear missing is set to false
Thank you @xabbuh, your fix fixes the issue for me |
I've created
TestFormType
created the form and trying to submit data
and now
$form->getData()
will return an array:I expect to receive
bar
for both fields.I'm working on the latest version of 4.4 branch
See the reproducer for this issue https://github.com/ossinkine/symfony-issue-16802
The text was updated successfully, but these errors were encountered: