Skip to content

Strange behavior of checkbox / switch fields #31

@xVinniVx

Description

@xVinniVx

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions