【问题标题】:Symfony 1.4 embedded form fields at the same indentSymfony 1.4 在同一缩进处嵌入表单字段
【发布时间】:2012-06-20 12:41:26
【问题描述】:

我的 Symfony 应用程序中有两个模型。第一个是博客:

Blog:
  columns:
    name: { type: string(20), notnull: true, unique: true }
    title: { type: string(255), notnull: true }
    description: { type: string(255), notnull: true }
    admin_id: { type: bigint, notnull: true }
  relations:
    User:
      class: sfGuardUser
      local: admin_id
      ...

如您所见,此模型与 sfGuardUser 具有一对一的关系。我希望这两个注册以一种形式进行。

所以我更改了 BlogForm 类并在其中使用了embeddedRelation 方法。所以两种形式只是一起出现。问题是他们的观点!用户注册表单(嵌入在 BlogForm 中)看起来像个孩子!我不希望这样...我希望字段具有相同的缩进。

我的表单视图是这样的:

但我想要这样的东西:

最好的方法是什么?和FormFormatter有关系吗?

【问题讨论】:

标签: php symfony1 symfony-1.4 symfony-forms


【解决方案1】:

您检查过sfWidgetFormSchemaFormatterrender* method 吗?

我给出了一个几乎相关的答案here。而且我认为这几乎与这里出现的问题相同:Removing table headers from embedRelation()

我认为最好的方法是使用sfWidgetFormSchemaFormatter或render*方法在模板中手动构建表单。


编辑:

关于我回答 here 的内容,尝试添加这样的自定义格式化程序(在 lib/widget/sfWidgetFormSchemaFormatterAc2009.class.php 中):

class sfWidgetFormSchemaFormatterAc2009 extends sfWidgetFormSchemaFormatter
{
  protected
    // this will remove table around the embed element
    $decoratorFormat = "%content%";

  public function generateLabel($name, $attributes = array())
  {
    $labelName = $this->generateLabelName($name);

    if (false === $labelName)
    {
      return '';
    }

    // widget name are usually in lower case. Only embed form have the first character in upper case
    if (preg_match('/^[A-Z]/', $name))
    {
      // do not display label
      return ;
    }
    else
    {
      return $this->widgetSchema->renderContentTag('label', $labelName, $attributes);
    }
  }
}

然后将其添加到ProjectConfiguration的表单中:

class ProjectConfiguration extends sfProjectConfiguration
{
  public function setup()
  {
    // ...

    sfWidgetFormSchema::setDefaultFormFormatterName('ac2009');
  }
}

(信息来自sf website

如果这不起作用,请在if (preg_match 之前添加var_dump($name); 并将输出添加到您的问题中。

【讨论】:

  • 感谢您的回复,但我不想手动操作。顺便问一下,在FormFormatter中,如何防止嵌入表单的头部回显?
  • 我不知道否则我会做出不同的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多