php - symfony 2 Dynamic drop down menus -


how crate dynamic drop down lists using symfony 2 have this,

$builder->add('gender', 'choice', array(          'choices'  => array(1 => 'campus1', 2 => 'campus2'),          'required' => false, )); 

i dont want hard code , dont want on twig or front end side, have someting in controller. here entity needs loaded drop down

<?php  namespace test\bundle\testbundle\entity;  use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert;  /**  * campus  *  * @orm\table()  * @orm\entity(repositoryclass="..\entity\campusrepository")  */ class campus { /**  * @var integer  *  * @orm\column(name="id", type="integer")  * @orm\id  * @orm\generatedvalue(strategy="auto")  */ private $id;  /**  * @orm\manytoone(targetentity="university", inversedby="campus")  * @orm\joincolumn(name="university_id", referencedcolumnname="id")  */ private $university;  /**  * @var string  *  * @orm\column(name="name", type="string", length=255)  * @assert\notblank  * @assert\length(  *                  min=3,  *                  max=35,  *                  minmessage= "name field should contains @ least 3 characters",  *                  maxmessage = "name field cannot contain more 35 characters"  *               )  * @assert\regex(pattern="/[^a-z\s-]/i", match=false , message="name can contain letters")  */ private $name;  /**  * @var string  * @orm\column(name="address", type="string", length=255)  * @assert\notblank()  * @assert\length(  *                 min = 10,  *                 max = 80,  *                 minmessage = "address must more specific",  *                 maxmessage = "80 characters limit exceeded"  *              )  */ private $address;   /**  * id  *  * @return integer   */ public function getid() {     return $this->id; }  /**  * set name  *  * @param string $name  * @return campus  */ public function setname($name) {     $this->name = $name;      return $this; }  /**  * name  *  * @return string   */ public function getname() {     return $this->name; }  /**  * set address  *  * @param string $address  * @return campus  */ public function setaddress($address) {     $this->address = $address;      return $this; }  /**  * address  *  * @return string   */ public function getaddress() {     return $this->address; }  /**  * set university  *  * @param \test\bundle\testbundle\entity\university $university  * @return campus  */ public function setuniversity(\test\bundle\testbundle\entity\university $university = null) {     $this->university = $university;      return $this; }  /**  * university  *  * @return \test\bundle\testbundle\entity\university   */ public function getuniversity() {     return $this->university; } 

}

when drop down selected , form submitted using other fields entity need entire object when form submitted.

so how populate data datbase using symfony2 features?

use entity form field type, special type of choice field that's designed load options doctrine entity. e.g.:

$builder->add('campus', 'entity', array(     'class' => 'testbundletestbundle:campus',     'property' => 'name',     'query_builder' => function (entityrepository $er) {         return $er->createquerybuilder('c')             ->orderby('c.name', 'asc');     }, )); 

when form submitted, form data 'campus' field instance of campus class. if form data entity rather array, entity need have corresponding 'campus' field represents relationship campus entity.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -