flowbite-bundle icon indicating copy to clipboard operation
flowbite-bundle copied to clipboard

Strange behavior of checkbox / switch fields

Open xVinniVx opened this issue 1 year ago • 1 comments

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?

xVinniVx avatar Sep 02 '24 17:09 xVinniVx

@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!

ker0x avatar Feb 05 '25 15:02 ker0x