0

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

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

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

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

4

2 回答 2

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()

我在此博客文章的第 5 步中有一个示例:

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

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

于 2010-12-14T01:15:27.693 回答
0

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

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

于 2010-12-13T20:13:25.673 回答