user742736
user742736

Reputation: 2729

Symfony: Using a type='text' with form type 'checkbox'

Is there a way to use the checkbox form type with a field that has been set to string?

This field can hold many values, it can be used to input data as a textfield or as a checkbox.

I have a event listener on the formtype to check if it should be a checkbox or text field.

/Entity.php

/**
 * @var string 
 *
 * @ORM\Column(name="value", type="string", length=200, nullable=true)
 */
private $value;

formtype.php

$form->add('value', 'checkbox', array(

))

Error

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

Upvotes: 2

Views: 4568

Answers (2)

arcos.lwm
arcos.lwm

Reputation: 101

this worked for me:

rewrite the function in the Entity, adding explicitly boolean values in returns

public function getEstado() {
    if ($this->estado===1) {
        return TRUE;
    }
    return FALSE;
}

Upvotes: 0

Evgeniy Budanov
Evgeniy Budanov

Reputation: 196

Entity.php

public function getValue()
{
    return (boolean)$this->value;
}

Upvotes: 6

Related Questions