Symfony:如何隐藏表单字段的显示,然后在操作类中为其设置值

发布于 2024-09-02 14:54:20 字数 1010 浏览 5 评论 0原文

我对 symfony 相当陌生,我有 2 个与我的表“Pages”相关的字段;创建者和更新者。这些与用户表 (sfGuardUser) 作为外键相关。我希望这些字段从编辑/新表单中隐藏,因此我已将生成器.yml 文件设置为不显示这些字段:

form:
  display:
    General: [name, template_id]
    Meta: [meta_title, meta_description, meta_keywords]

现在我需要在保存时设置字段。我一整天都在寻找如何做到这一点,并尝试了一百种方法。我在 actions 类中使用的方法是这样的:

protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form_params = $request->getParameter($form->getName());
    $form_params['updated_by'] = $this->getUser()->getGuardUser()->getId();
    if ($form->getObject()->isNew()) $form_params['created_by'] = $this->getUser()->getGuardUser()->getId();

    $form->bind($form_params, $request->getFiles($form->getName()));

所以这有效。但我感觉理想情况下我不应该修改网络请求,而是直接修改表单/对象。然而,我在以下方面没有取得任何成功:

$form->getObject()->setUpdatedBy($this->getUser()->getGuardUser());

如果有人能够就解决此类问题的最佳方法提供任何建议,我将非常感激。

谢谢, 汤姆

I am fairly new to symfony and I have 2 fields relating to my table "Pages"; created_by and updated_by. These are related to the users table (sfGuardUser) as foreign keys. I want these to be hidden from the edit/new forms so I have set up the generator.yml file to not display these fields:

form:
  display:
    General: [name, template_id]
    Meta: [meta_title, meta_description, meta_keywords]

Now I need to set the fields on the save. I have been searching for how to do this all day and tried a hundred methods. The method I have got working is this, in the actions class:

protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form_params = $request->getParameter($form->getName());
    $form_params['updated_by'] = $this->getUser()->getGuardUser()->getId();
    if ($form->getObject()->isNew()) $form_params['created_by'] = $this->getUser()->getGuardUser()->getId();

    $form->bind($form_params, $request->getFiles($form->getName()));

So this works. But I get the feeling that ideally I shouldnt be modifying the web request, but instead modifying the form/object directly. However I havent had any success with things like:

$form->getObject()->setUpdatedBy($this->getUser()->getGuardUser());

If anyone could offer any advice on the best ways about solving this type of problem I would be very grateful.

Thanks,
Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷默言语 2024-09-09 14:54:20

处理并保存表单后,您可以在对象上设置这些字段并重新保存:

protected function processForm(sfWebRequest $request, sfForm $form)
{
  $form->bind($request->getParameter($form->getName()));

  if ($form->isValid())
  {
    $page = $form->save();
    $user = $this->getUser()->getGuardUser();
    $page->setUpdatedBy($user);
    if (empty($page->created_by))
    {
      $page->setCreatedBy($user);
    }

    $page->save();

    $this->getUser()->setFlash('notice', 'Successfully saved page.');
    $this->redirect('@homepage');
  }
}

还有一个名为 Blameable 的 Doctrine 扩展,可以自动在指定模型上设置edited_by 和created_by 字段。 Doctrine 网站正在进行一些重组,但这里是 扩展程序的缓存页面

After processing and saving the form you could set those fields on the object and re-save:

protected function processForm(sfWebRequest $request, sfForm $form)
{
  $form->bind($request->getParameter($form->getName()));

  if ($form->isValid())
  {
    $page = $form->save();
    $user = $this->getUser()->getGuardUser();
    $page->setUpdatedBy($user);
    if (empty($page->created_by))
    {
      $page->setCreatedBy($user);
    }

    $page->save();

    $this->getUser()->setFlash('notice', 'Successfully saved page.');
    $this->redirect('@homepage');
  }
}

There's also a Doctrine extension called Blameable that automatically sets edited_by and created_by fields on specified models. The Doctrine website is undergoing some reorganization but here is the cached page for the extension.

怎会甘心 2024-09-09 14:54:20

要处理表单,请创建一个新对象,设置字段然后保存。

$article = new Article();
$article->setName($request->getParameter($form->getName());
$article->setDescription($request->getParameter($form->getDescription());
$article->setMetaKeywords($request->getParameter($form->getMetaKeywords());
$article->save();

To process your form create a new object, set the fields then save.

$article = new Article();
$article->setName($request->getParameter($form->getName());
$article->setDescription($request->getParameter($form->getDescription());
$article->setMetaKeywords($request->getParameter($form->getMetaKeywords());
$article->save();
记忆之渊 2024-09-09 14:54:20

您想要做的是自定义表单并在配置中取消设置表单的“created_at”和“updated_at”部分

class SampleForm extends BaseSampleForm
{   
  public function configure()
  {
    unset(
      $this['created_at'],
      $this['updated_at']
    );
  }
}

然后它们不会显示在表单中,并在保存之前通过“Timestampable”行为获取设置的值

http://stereointeractive.com/blog/ 2010/04/07/symfony-forms-hide-created_at-updated_at-columns/

What you want to do is customize your form and unset the 'created_at' and 'updated_at' pieces of the form in configure

class SampleForm extends BaseSampleForm
{   
  public function configure()
  {
    unset(
      $this['created_at'],
      $this['updated_at']
    );
  }
}

Then they won't show up in the form and will get the values setup by the "Timestampable" behavior before being saved

http://stereointeractive.com/blog/2010/04/07/symfony-forms-hide-created_at-updated_at-columns/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文