Skip to content
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

Strange behavior of checkbox / switch fields #31

Open
xVinniVx opened this issue Sep 2, 2024 · 1 comment
Open

Strange behavior of checkbox / switch fields #31

xVinniVx opened this issue Sep 2, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@xVinniVx
Copy link

xVinniVx commented Sep 2, 2024

Hi,
I noticed a strange (erroneous?) behavior of checkbox / switch fields.

If we have a form that is sent and validated with errors - each checked checkbox field will be marked red - as if there was a validation error associated with them (even if it is not).
Here is how the setup looks like:

  • Entity
class Testowy
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Assert\NotBlank(message: 'To pole jest wymagane')]
    private ?string $name = null;

    #[ORM\Column(length: 40)]
    #[Assert\NotBlank(message: 'To pole jest wymagane')]
    private ?string $city = null;

    #[ORM\Column(length: 100, nullable: true)]
    private ?string $street = null;

    #[ORM\Column(nullable: true)]
    private ?bool $forDelivery = null;
  • FormType
public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name')
            ->add('city')
            ->add('street')
            ->add('forDelivery', CheckboxType::class)
            ->add('test', CheckboxType::class, [
                'mapped' => false,
                'required' => false,
            ])
        ;
    }
  • Twig
    <h1 class="text-white font-bold py-4">Create/Edit Testowy</h1>

    {{ form_start(form, {'attr': {'novalidate': 'novalidate'}}) }}
    <div>{{ form_row(form.name) }}</div>
    <div>{{ form_row(form.city) }}</div>
    <div>{{ form_row(form.street) }}</div>
    <div>{{ form_row(form.forDelivery) }}</div>
    <div>{{ form_row(form.test) }}</div>

Form with validation errors without checked checkboxes:
image

Form with error s with checked checkboxes (they should not be marked red):
image

Note that the TEST field is not mapped to an entity. It should not be grabbed by the validator in any way.

Validation result from Profiler:
image

Why is this happening? Is it me who doesn't understand something? In the version without FlowbiteBundle - the boxes are not marked in red.

What do I need and where to modify to deal with this problem?

@ker0x ker0x added the bug Something isn't working label Feb 5, 2025
@ker0x
Copy link
Member

ker0x commented Feb 5, 2025

@xVinniVx Sorry for the late reply!

It seems to come from this line: https://github.com/tales-from-a-dev/flowbite-bundle/blob/main/templates/form/default.html.twig#L155

I'll have to take a closer look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants