1

我使用 embedRelation() 将另一个表单拉入 sfGuard 用户表单。它工作正常,我只是想设计它以适应当前的形式。

这会将表单拉入:

$this->embedRelation('Profile');

但它添加了一些我想删除的额外 html 元素:

<div class="form_row">
 Profile 
 <table>
  <tbody><tr>
  <th><label for="sf_guard_user_Profile_department_title">Department</label></th>
  <td><input type="text" name="sf_guard_user[Profile][department_title]" value="Graphic Design" id="sf_guard_user_Profile_department_title"><input type="hidden" name="sf_guard_user[Profile][id]" value="2" id="sf_guard_user_Profile_id">
<input type="hidden" name="sf_guard_user[Profile][sf_guard_user_id]" value="10" id="sf_guard_user_Profile_sf_guard_user_id"></td>
</tr>
</tbody></table> <input type="hidden" name="sf_guard_user[id]" value="10" id="sf_guard_user_id"> </div>

如何删除“个人资料”?我还希望标签不包含在表头中。这可以使用 embedRelation 吗?

更新的模式格式化程序:

sfWidgetFormSchemaFormatter 用于从嵌入表单中删除表格元素。我仍然无法弄清楚如何摆脱“个人资料”。我添加了 sfWidgetFormSchemaFormatter

sfWidgetFormSchemaFormatterAc2009.class.php

    <?php 

    class sfWidgetFormSchemaFormatterAc2009 extends sfWidgetFormSchemaFormatter
    {
      protected
        $rowFormat       = "%error% \n %label% \n %field%
                            %help% %hidden_fields%\n",
        $errorRowFormat  = "<div>%errors%</div>",
        $helpFormat      = '<div class="form_help">%help%</div>',
        $decoratorFormat = "%content%";

        public function formatRow($label, $field, $errors = array(), $help = '', $hiddenFields = null)
        {
          $row = parent::formatRow(
            $label,
            $field,
            $errors,
            $help,
            $hiddenFields
          );

          return strtr($row, array(
            '%row_class%' => (count($errors) > 0) ? ' form_row_error' : '',
          ));
        }

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

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

        if (!isset($attributes['for']))
        {
          $attributes['for'] = $this->widgetSchema->generateId($this->widgetSchema->generateName($name));
        }

        // widget name are usually in lower case. Only embed form have the first character in upper case

        var_dump($name);

        if (preg_match('/^[A-Z]/', $name))
        {
          // do not display label
          return ;
        }
        else
        {
          return $this->widgetSchema->renderContentTag('label', $labelName, $attributes);
        }
      }
    }

html:

<div class="form_row">
 Profile 
 <div>

 <label for="sf_guard_user_Profile_department_title">Department</label> 
 <input class=" text size-300" type="text" name="sf_guard_user[Profile][department_title]" value="Graphic Design" id="sf_guard_user_Profile_department_title">
                         <input type="hidden" name="sf_guard_user[Profile][id]" value="2" id="sf_guard_user_Profile_id">
</div> <input type="hidden" name="sf_guard_user[id]" value="10" id="sf_guard_user_id"> </div>

更新 2:

新函数 generateLabel($name, $attributes = array()) 在表单顶部生成:

字符串(16)“部门标题”

配置文件仍然存在。

解决

我能够使用 JQuery 完成这个

我将此添加到 editSuccess.php 模板中:

<script>
var $body = $('.content-box');
var html = $body.html();
var newHtml = html.replace('Profile', '');
$body.html(newHtml);
</script>
4

2 回答 2

1

看看sfWidgetFormSchemaFormatter- 也在这里描述

于 2012-06-20T11:14:03.783 回答
1

我找到了一个适合我的工作。这是一个有点奇怪的解决方案。

标签使用 显示generateLabel。所以,如果我们想隐藏标签,我们只有标签名称来构建条件。

在您的自定义格式化程序中:

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

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

    if (!isset($attributes['for']))
    {
      $attributes['for'] = $this->widgetSchema->generateId($this->widgetSchema->generateName($name));
    }

    // 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);
    }
  }
于 2012-06-22T20:06:04.827 回答