4

New to symfony2, I have a simple table with 2 fields.

As alert field is a boolean, I declared the form like this:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder                                
        ->add('message', 'text', array('label' => "Message"))                
        ->add('alert', 'choice', array(
              'choices'   => array(1 => 'Yes', 0 => 'No'),
              'expanded'  => true,
              'multiple'  => false,
              'label'     => "Are you agree?",
              'attr'      => array('class' => 'well')
        ));
}

It is working when I create a new entry, but when I am trying to edit the entry, the 'alert' choice stored in database is not set in the form (radio button).

How can I set the database state of the field in the form?

1
  • Is it an entity form ? Commented Aug 19, 2015 at 15:19

2 Answers 2

3

You have 2 options here.

Try to use the data attribute in the formbuilder.

$builder                                
        ->add('message', 'text', array('label' => "Message"))                
        ->add('alert', 'choice', array(
              'choices'   => array(1 => 'Yes', 0 => 'No'),
              'expanded'  => true,
              'multiple'  => false,
              'label'     => "Are you agree?",
              'data'      => $entity->getAlert(),
              'attr'      => array('class' => 'well')
        ));

Or: When creating the form in symfony you usually pass along the data entity to that form. This auto fills all the values.

$this->createForm(new FormType(), $entity);
Sign up to request clarification or add additional context in comments.

3 Comments

OK, is there no way for Symfony to set the database value as 'data' automatically?
@sdespont It is done automatically ... you just have to get the entity and set it as the second parameter of your createForm as shown above
@Rico Humme I understand, but editing an "entity" field for exemple doesn't need to specify the existing database value, Symfony just set it. In that case, I have to retrieve the value first (if the entity exists) and then give the value to the 'data' field. In my point of view, it is not done automatically.
1

To complete Rico Humme's answer, here is how you would do it.

public function myFunc() {
    ....
    $entity = $this->getDoctrine()
        ->getRepository('AcmeFooBundle:Entity')
        ->find($id);
    if ($entity) {
        $form = $this->createForm(new EntityType(), $entity);
        ...
    }
}

EDIT:

To complete my answer, here is what EntityType could look like:

class EntityType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //This is just soe
        $builder->add('alert', 'choice', array(
          'choices'   => array(1 => 'Yes', 0 => 'No'),
          'expanded'  => true,
          'multiple'  => false,
          'label'     => "Are you agree?",
          'attr'      => array('class' => 'well')
        ));
    }

    public function getName()
    {
        return 'entity';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\FooBundle\Entity\Entity',
        ));
    }

}

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.