Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony boolean field into form

Tags:

php

symfony

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?

like image 399
dontHaveName Avatar asked Oct 19 '15 08:10

dontHaveName


2 Answers

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

/**
 * @ORM\Column(type="boolean")
 */
protected $done = false;
like image 111
scoolnico Avatar answered Sep 17 '22 07:09

scoolnico


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

like image 43
vimuth Avatar answered Sep 20 '22 07:09

vimuth