php - Symfony2 : set default value from database in radio buttons choice form? -
new symfony2, have simple table 2 fields.
as alert
field boolean, declared form 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 agree?", 'attr' => array('class' => 'well') )); }
it working when create new entry, when trying edit entry, 'alert' choice stored in database not set in form (radio button).
how can set database state of field in form?
you have 2 options here.
try use data attribute in formbuilder.
$builder ->add('message', 'text', array('label' => "message")) ->add('alert', 'choice', array( 'choices' => array(1 => 'yes', 0 => 'no'), 'expanded' => true, 'multiple' => false, 'label' => "are agree?", 'data' => $entity->getalert(), 'attr' => array('class' => 'well') ));
or: when creating form in symfony pass along data entity form. auto fills values.
$this->createform(new formtype(), $entity);
Comments
Post a Comment