symfony - OneToMany relationship with checkboxes as choices in a form using custom FormType -
i have model costtypeconnection has 1 many relation departmentconnection.
i want, departmentconnection displayed checkboxes.
my costtypeconnection class
has variable $costtype
, entity costtype
, has $name
varaible in it.
my departmentconnection class
has variable $department
, entity department
, has $name
variable.
my costtypeformtype:
.... public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('costtype', 'text', array( 'label' => 'costtype' )) ->add('departmentconnections', 'collection', array( 'type' => new departmentconnectiontype(), 'allow_add' => false, 'label' => false, 'by_reference' => false, 'options' => array('label' => false), )) ; } ...
my departmentconnection:
... public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('department', 'text', array( 'label' => false )) ; }...
so not possible define checkbox-type in formbuilder department, because entity , not variable. has idea can do? doesnt work in departmentconnectiontype class:
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('department', 'choice', array( 'label' => false )) ; }
it finishs error:
the value of type "object" cannot converted valid array key
thanks help!!!
note : recommended minimum version symfony 2.7
you should use \symfony\bridge\doctrine\form\type\entitytype in form.
see http://symfony.com/doc/2.7/reference/forms/types/choice.html , http://symfony.com/doc/2.7/reference/forms/types/entity.html
here's example :
public function buildform(formbuilderinterface $builder, array $options) { $builder ->add('costtype', 'text', array( 'label' => 'cost type :' // -> <label class="required">cost type :</label><input value="<?php echo $costtypeconnection->getcosttype(); ?> required> )) ->add('departmentconnections', 'entity', array( 'class' => 'acme\bundle\entity\departmentconnection', // before 2.7 use 'property' instead of 'choice_label' 'choice_label' => 'type', // equals $costtypeconnections->getdepartmentconnexion()->gettype(); 'label' => 'department connexion :', 'expanded' => true, // output set of inputs instead of select tag 'multiple' => true, // output checkboxes instead of radio buttons 'required' => true, // unused multiple true )) ; } ...
would output :
<div id="form_cost_type_connexion_department_connections"> department connexion : <input type="checkbox" value="on" ... <label>$costtypeconnections->getdepartmentconnections()->gettype() ...
Comments
Post a Comment