Symfony 1.4 以相同的缩进嵌入表单字段 [英] Symfony 1.4 embedded form fields at the same indent

查看:17
本文介绍了Symfony 1.4 以相同的缩进嵌入表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two models in my Symfony application. First one is Blog:

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 具有一对一的关系.我希望这两个注册以一种形式进行.

As you can see this model has a one-to-one relationship with sfGuardUser. I want the registration of these two take place in one form.

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

So I changed the BlogForm class and used embeddedRelation method in it. So two forms just appear together. The problem is their view! User registration form (which is embedded in BlogForm) seems like child! I don't want this... I want the fields be at the same indent.

我的表单视图是这样的:

My form view is like this:

但我想要这样的东西:

这样做的最佳方法是什么?和 FormFormatter 有关系吗?

What is the best way to do this? Does it relate to FormFormatter?

推荐答案

您是否检查了 sfWidgetFormSchemaFormatterrender* 方法 ?

Have you check sfWidgetFormSchemaFormatter or render* method ?

我在此处回答了几乎相关的问题.我认为这几乎与这里出现的问题相同:从 embedRelation() 中删除表头

I gave an answer for something almost related here. And I think it's almost the same problem that came here: Removing table headers from embedRelation()

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

I think the best way is to manually build the form in the template using sfWidgetFormSchemaFormatter or render* method.

关于我在此处的回答,尝试添加这样的自定义格式化程序(在 lib/小部件/sfWidgetFormSchemaFormatterAc2009.class.php) :

Regarding what I've answered here, try to add a custom formatter like this (in 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 表单中:

Then add it to your form in ProjectConfiguration:

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

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

(信息来自sf网站)

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

If this doesn't work, please add a var_dump($name); before the if (preg_match and add the output to your question.

这篇关于Symfony 1.4 以相同的缩进嵌入表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆