Minirock
Minirock

Reputation: 666

SonataAdmin create entity with boolean field

I have this entity, if I create a record like this.

$synopsis = new Synopsis();
$synopsis->setPartOne("a");
$synopsis->setPartTwo("b");
$synopsis->setTitle("A");
$synopsis->setSubtitle("B");
$synopsis->setEnabled(false);

$em->persist($synopsis);
$em->flush();

And then I go to my Admin, I see the enabled field to "no" which is expected.

But now, If I use the sonata admin new form field, even if I choose enabled "no", the record is created with enabled = true. And I don't really see why it would be like that.

Here is what I have in my SynopsisAdmin

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper->add('title', TextType::class);
    $formMapper->add('subtitle', TextType::class);
    $formMapper->add('partOne', TextAreaType::class);
    $formMapper->add('partTwo', TextAreaType::class);
    $formMapper->add('enabled', BooleanType::class);
}

This is how the enabled field is defined in the entity

/**
 * @ORM\Column(type="boolean")
 */
private $enabled;

Thanks for your help.

EDIT: Fun facts too, even if I see no in the sonata view list, when I go to the form view, I see yes instead.

I suspect an error within the sonata core functionnality.

Upvotes: 0

Views: 1175

Answers (1)

Jeroen
Jeroen

Reputation: 2031

I think you should use the CheckboxType instead of the BooleanType for your Form fields.

Looks like the BooleanType is ment for the list, show and grid actions. https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/field_types.html

Update

To use the BooleanType you have to set the 'transform' option to true. This transforms your boolean value to the YES/NO options in the BooleanType:

$formMapper
    ->add('enabled', BooleanType::class, [
           'transform' => true
    ])

Upvotes: 2

Related Questions