dontHaveName
dontHaveName

Reputation: 1937

symfony boolean field into form

I have this field in entity:

/**
* @ORM\Column(type="boolean")
*/
protected $done = 0;

In database it's tinyint(1). When I try to add it into a form:

$builder
   ->add('done', 'checkbox')

It throws an error:

Unable to transform value for property path "done": Expected a Boolean.

Huh? Isn't it boolean?

Upvotes: 17

Views: 16816

Answers (2)

vimuth
vimuth

Reputation: 5612

thanks a lot for the solution but this didn't work for me. I use symfony 4. This is how I accomplished,

Entity,

/**
* @ORM\Column(type="boolean")
*/
protected $done = 0;

public function getDone(): ?bool
{
    return $this->done;
}

public function setDone(?bool $done): self
{
    $this->done = $done;

    return $this;
}

FormType,

->add('done', CheckboxType::class, array(
    'required' => false,
    'value' => 1,
))

I needed to add use CheckboxType since I call the class. (use Symfony\Component\Form\Extension\Core\Type\CheckboxType;) If you run "php bin/console doctrine:migrations:diff" db will add tinyint(1) field

Upvotes: 8

scoolnico
scoolnico

Reputation: 3135

0 or 1 are not booleans. They are integers. Use true/false in your domain model.

/**
 * @ORM\Column(type="boolean")
 */
protected $done = false;

Upvotes: 22

Related Questions