Symfony Admin Generator: Add User ID Before Saving

I create my own blog engine to learn Symfony, and I have a question:

In the generated admin pages for the blog post, I have a drop-down list of authors to specify author_id.

I would like to hide this drop-down list and set the author_id identifier to the identifier of the current user in the system when the message is created (but not when it is being edited).

How can i do this?

Edit I tried:

$request->setParameter(sprintf("%s[%s]", $this->form->getName(), "author_id"), $this->getUser()->getAttribute("user_id")); $request->setParameter("content[author_id]", $this->getUser()->getAttribute("user_id")); $request->setParameter("author_id", $this->getUser()->getAttribute("user_id")); $request->setParameter("author_id", 2); $request->setParameter("content[author_id]", 2); $request->setParameter("author_id", "2"); $request->setParameter("content[author_id]", "2"); 

In processForm () and executeCreate ()

Solved!

End Code:

  public function executeCreate(sfWebRequest $request) { $form = $this->configuration->getForm(); $params = $request->getParameter($form->getName()); $params["author_id"] = $this->getUser()->getGuardUser()->getId();; $request->setParameter($form->getName(), $params); parent::executeCreate($request); } 
+2
source share
3 answers

Override the executeCreate function in the action file. When binding post data to the form, merge the current user ID into it.

Second update

I experimented and it works:

 class fooActions extends autoFooActions { public function executeCreate(sfWebRequest $request) { $form = $this->configuration->getForm(); $params = $request->getParameter($form->getName()); $params["author_id"] = 123; $request->setParameter($form->getName(), $params); parent::executeCreate($request); } } 
+2
source

change the widget on the form using sfWidgetFormInputHidden and set the value using the sfUser attribute (defined when the user logs in)

override executeCreate () and set the author_id widget (thanks maerlyn: D)

 public function executeCreate(sfWebRequest $request){ parent::executeCreate($request); $this->form->setWidget('author_id', new sfWidgetFormInputHidden(array(),array('value'=>$this->getUser()->getAttribute('author_id'))) ); } 
0
source

In objects, the solution is: (new and $ this)

 class fooActions extends autoFooActions { public function executeCreate(sfWebRequest $request) { $this->form = new XxxxxForm(); $params = $request->getParameter($this->form->getName()); $params["author_id"] = 123; $request->setParameter($this->form->getName(), $params); parent::executeCreate($request); } } 
0
source

Source: https://habr.com/ru/post/1336023/


All Articles