Removing table headers from embedRelation()

半世苍凉 提交于 2019-12-11 11:07:22

问题


I have used the embedRelation() to pull another form into the sfGuard user form. It is working correctly, I am just trying to style it to fit into the current form.

This pulls the form in:

$this->embedRelation('Profile');

but it is adding some extra html elements that I would like to remove:

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

How can I remove "Profile"? I would also like the label to not be wrapped in a table header. Is this possible using embedRelation?

UPDATED Schema Formatter:

sfWidgetFormSchemaFormatter works to remove the table elements from the embedded form. I still can't figure out how to get rid of "Profile". I have added the 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>

UPDATE 2:

The new function generateLabel($name, $attributes = array()) is generating this at the top of the form:

string(16) "department_title"

Profile remains.

RESOLVED

I was able to acomplish this using JQuery

I added this to the editSuccess.php template:

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

回答1:


I found a work around that works for me. It's a bit weird solution.

The label is displayed using generateLabel. So, if we want to hide the label, we only have the label name to build a condition.

In your custom formatter:

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



回答2:


Take a look at sfWidgetFormSchemaFormatter - also described here



来源:https://stackoverflow.com/questions/11085965/removing-table-headers-from-embedrelation

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