【问题标题】:Admin form with "extra" fields带有“额外”字段的管理表单
【发布时间】:2010-12-13 17:10:05
【问题描述】:

我有一个名为AccountImport 的对象的表单。此表单位于管理员生成的模块中。除了直接映射到该对象属性的字段外,我还需要几个额外的字段。

如果我只是将字段添加到 AccountImport 表单,它不会正确保存,因为表单将不再匹配 AccountImport 对象。

如果我手动创建模板并以这种方式拼接额外的字段,我将丢弃管理员生成器免费提供的所有内容(即格式化、“返回列表”按钮、保存按钮)。

什么是做我想做的事情的“好”方法?

【问题讨论】:

  • 欢迎来到框架成为负担的地方。希望其他人已经解决了这个问题......
  • 普肖@DampeS8N。 Symfony 实际上非常优雅地处理了这个问题。
  • 我的意思是更宏大的计划。如果你的框架没有做你想做的事,那是世界上最糟糕的感觉。你只需要向巴尔和贝利亚祈祷,让其他人修复它。如果这不是交响乐的垮台。别的东西是。 :)

标签: php symfony1 symfony-forms


【解决方案1】:

如果您在 generator.yml 中定义其他字段,则可以覆盖管理员生成器操作之一来处理您想要的字段。

cache/YOURAPP/YOURENV/modules/autoYOURMODULE/actions/actions.class.php 中查看生成的 actions.class.php。您可以在 apps/YOURAPP/modules/YOURMODULE/actions/actions.class.php 中用自己的函数覆盖任何这些函数,因为它继承自该缓存文件。当您对 generator.conf 进行更改时,缓存文件会更新,但您的代码仍会覆盖它。您可能想要覆盖processForm()

我在step 5 at this blog post 中有一个这样的例子:

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

  if ($form->isValid())
  {
$notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';

// NEW: deal with tags
if ($form->getValue('remove_tags')) {
  foreach (preg_split('/\s*,\s*/', $form->getValue('remove_tags')) as $tag) {
    $form->getObject()->removeTag($tag);
  }
}
if ($form->getValue('new_tags')) {
  foreach (preg_split('/\s*,\s*/', $form->getValue('new_tags')) as $tag) {
    // sorry, it would be better to not hard-code this string
    if ($tag == 'Add tags with commas') continue;
    $form->getObject()->addTag($tag);
  }
}

try {
  $complaint = $form->save();
  // and the remainder is just pasted from the generated actions file

当我意识到我可以读取缓存中生成的文件以确切了解管理生成器在做什么,并且我可以覆盖其中的任何部分时,使用管理生成器让我的工作效率大大提高。

【讨论】:

    【解决方案2】:

    我假设您已将额外字段作为小部件添加到表单对象,但您添加了它们的验证器吗?

    无论您在表单对象中包含哪些表单字段,只要generator.yml 文件不覆盖表单的设置(即您没有为该文件中的[new|form|edit].display 键设置任何值) 对象应成功保存在有效输入上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 2014-09-03
      • 2012-02-11
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      相关资源
      最近更新 更多