在您的 /lib/form 文件夹中创建另一个表单来扩展您的正常表单,然后覆盖相应的字段。以下将从表单中删除该字段,使其根本不显示。
<?php
class FrontendSomeModelForm extends SomeModelForm {
public function configure()
{
unset($this["some_field"]);
}
}
或者,如果您想要呈现该值,但不允许对其进行编辑,您可以执行以下操作:
<?php
class FrontendSomeModelForm extends SomeModelForm {
public function configure()
{
$this->setWidget("some_field", new sfWidgetFormPlain());
}
}
然后创建一个sfWidgetFormPlain
只输出值的小部件并将其粘贴到 symfony 可以找到它的地方(lib/form/widget 或其他东西)。
<?php
class sfWidgetFormPlain extends sfWidgetForm
{
/**
* Constructor.
*
* @param array $options An array of options
* @param array $attributes An array of default HTML attributes
*
* @see sfWidgetForm
*/
protected function configure($options = array(), $attributes = array())
{
$this->addOption('value');
}
/**
* @param string $name The element name
* @param string $value The value displayed in this widget
* @param array $attributes An array of HTML attributes to be merged with the default HTML attributes
* @param array $errors An array of errors for the field
*
* @return string An HTML tag string
*
* @see sfWidgetForm
*/
public function render($name, $value = null, $attributes = array(), $errors = array())
{
//optional - for easy css styling
$attributes['class'] = 'plain';
return $this->renderContentTag('div', $value, $attributes);
}
}
然后,您使用此表单而不是普通表单来处理您不想编辑的表单。查看 symfony 文档以了解如何执行此操作,具体取决于您是在模块中显示它,还是通过管理生成器显示它。