symfony 2- fill up the form in controller -


i have form , fill 1 field (fromid) in controller (i used setfromid method), receives error. there code:

 public function newaction() {     $entity = new privatemessage();      $user = $this->container->get('security.context')->gettoken()->getuser();     $entity->setfromid($user);     $form   = $this->createform(new privatemessagetype(), $entity);     $form->setdata($entity);       return $this->render('acmestorebundle:privatemessage:new.html.twig', array(         'entity' => $entity,         'form'   => $form->createview(),     )); } 

do missed something? @edit: forgot add it's working when field fromid avaliable in form. don't want let user change it.

and part of privatemessage entity:

class privatemessage 

{/** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id;

/**  *  * @orm\manytoone(targetentity="user", inversedby="pm_to")  * @orm\joincolumns({  *   @orm\joincolumn(name="to_id", referencedcolumnname="id", nullable=false)  * })  */ private $toid;  /**  *  * @orm\manytoone(targetentity="user", inversedby="pm_from")  * @orm\joincolumns({  *   @orm\joincolumn(name="from_id", referencedcolumnname="id", nullable=false)  * })  */ private $fromid;   function __construct() {     $this->setopened(false);     $this->setrecipientdelete(false);     $this->setsenderdelete(false); } /**  * @orm\prepersist  */ public function setfromidvalue() {     $this->fromid= $this->getfromid(); }  /**  * set fromid  *  * @param \acme\bundle\entity\user $fromid  * @return privatemessage  */ public function setfromid(\acme\storebundle\entity\user $fromid = null) {     $this->fromid = $fromid;      return $this; }  /**  * fromid  *  * @return \acme\storebundle\entity\user   */ public function getfromid() {     return $this->fromid; } 

there better place controller set (even override) form fields - event listener. should write listener prepersist doctrine event , set/update field. please @ example docs: http://symfony.com/doc/current/cookbook/doctrine/event_listeners_subscribers.html#configuring-the-listener-subscriber


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -