我已经使用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吗?
UPDATED Schema Formatter:
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())在表单的顶部生成:
string(16)“department_title”
档案仍然存在。
分辨
我能够使用JQuery
来完成这项工作我将此添加到editSuccess.php模板中:
<script>
var $body = $('.content-box');
var html = $body.html();
var newHtml = html.replace('Profile', '');
$body.html(newHtml);
</script>
答案 0 :(得分:1)
查看sfWidgetFormSchemaFormatter
- 还介绍了here
答案 1 :(得分: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);
}
}