1

When I create a new entity, I want a certain checkbox to be checked by default. As the documentation says I used the data attribute, but now when I edit the same entity the checkbox is checked wether the value is 0 or 1.

How can I make it so that when I do a new entity, the checkbox is checked. but when I uncheck it, store the entity, and edit it, it uses the stored value ?

 $builder
            ->add(
                'naamOrganisatie',
                'text',
                array(
                    'label' => 'Naam organisatie',
                    'required' => false)
            )
            ->add('isOrganisatie', 'checkbox', array('required' => false, 'data' => true))
            ->add('naam', 'text', array('required' => false))
            ->add('voornaam', 'text', array('required' => false))
            ->add('vestigingsnummer');

3 Answers 3

2

Init isOrganisatie data in entity constructor using:

public function __construct() {
    $this->isOrganisatie = true;
}

You are overriding the value using data attribute in form.

Sign up to request clarification or add additional context in comments.

3 Comments

/** * @ORM\Column(type="boolean", nullable=true) */ private $isOrganisatie = true; Did the trick
is it normal your are using the boolean comparator == in the constructor ?
That's was a typo, I fixed that
1

In Entity/Object.php file

public function __construct() {
    $this->setIsOrganisatie(true); 
}

Comments

0

You can initialize in Entity attribut for default value

/**
 * @var boolean
 *
 * @ORM\Column(name="is_organisatie", type="boolean", nullable=true)
 */
private $isOrganisatie = true;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.