Symfony 1.4 embedded form fields at the same indent

◇◆丶佛笑我妖孽 提交于 2019-12-08 06:44:17

问题


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
      ...

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.

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:

But I want something like this:

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


回答1:


Have you check sfWidgetFormSchemaFormatter or render* method ?

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

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


Edit:

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);
    }
  }
}

Then add it to your form in ProjectConfiguration:

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

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

(information comes from sf website)

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



来源:https://stackoverflow.com/questions/11119789/symfony-1-4-embedded-form-fields-at-the-same-indent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!