Zend framework2 ZF2表单中的按钮内容
如何编辑按钮元素(ZF2表单)的按钮内容? 我可以设置一个标签,但我想在其中插入一些html代码Zend framework2 ZF2表单中的按钮内容,zend-framework2,zend-form,zend-form-element,Zend Framework2,Zend Form,Zend Form Element,如何编辑按钮元素(ZF2表单)的按钮内容? 我可以设置一个标签,但我想在其中插入一些html代码 $this->add(array( 'type' => 'Button', 'name' => 'submit', 'options' => array( 'label' => 'Modifica', ), 'attributes' => array
$this->add(array(
'type' => 'Button',
'name' => 'submit',
'options' => array(
'label' => 'Modifica',
),
'attributes' => array(
'type' => 'submit',
'class' => 'btn btn-warning'
)
));
谢谢,正如@Sam正确提到的那样,
FormButton
view助手将自动退出按钮HTML内容
避免这种情况的唯一方法是使用自定义表单按钮视图帮助器。而不是删除转义功能(因为按钮文本内容仍应转义);您可以扩展视图帮助器并添加一个额外的选项,以允许您呈现html(我假设这是一个引导图标)
比如说
use Zend\Form\View\Helper\FormButton as ZendFormButton;
class FormButton extends ZendFormButton
{
public function render(ElementInterface $element, $buttonContent = null)
{
$content = (isset($buttonContent)) ? $buttonContent : $element->getLabel();
$icon = isset($element->getOption('icon')) ? $element->getOption('icon') : '';
$escape = $this->getEscapeHtmlHelper();
return $this->openTag($element) . $icon . $escape($content) . $this->closeTag();
}
}
然后在服务管理器中使用button view助手的默认注册名称(“form_button”)创建一个“可调用”配置条目。这将
然后表示将使用我们的视图帮助程序,而不是默认的Zend\Form\view\helper\FormButton
// Module.php
public function getViewHelperConfig()
{
return array(
'invokables' => array(
'form_button' => 'MyModule\Form\View\Helper\FormButton',
)
);
}
最后,更改按钮元素规格以添加新的“图标”选项
$this->add(array(
'type' => 'Button',
'name' => 'submit',
'options' => array(
'label' => 'Modifica',
'icon' => '<i class="icon icon-foo">',
),
'attributes' => array(
'type' => 'submit',
'class' => 'btn btn-warning'
)
));
$this->添加(数组)(
'类型'=>'按钮',
'名称'=>'提交',
“选项”=>数组(
'label'=>'Modifica',
'图标'=>'',
),
'属性'=>数组(
'类型'=>'提交',
'class'=>'btn btn警告'
)
));
其他几点
- 如果您使用按钮的翻译,则上述代码将删除该功能;这可以很容易地再次添加(只需查看按钮视图帮助器)。我删除了它以使示例更清晰
- 您还可以扩展它,向元素添加任意数量的其他选项(可能是按钮文本之前或之后的图标位置)。视图助手只需要读取此选项并输出正确的HTML
$this->add(array(
'type' => 'Button',
'name' => 'submit',
'options' => array(
'label' => '<i class="icon icon-foo"></i> Submit',
'label_options' => array(
'disable_html_escape' => true,
)
),
'attributes' => array(
'type' => 'submit',
'class' => 'btn btn-success'
)
));
$this->添加(数组)(
'类型'=>'按钮',
'名称'=>'提交',
“选项”=>数组(
“标签”=>“提交”,
“标签选项”=>数组(
'disable_html_escape'=>true,
)
),
'属性'=>数组(
'类型'=>'提交',
“类”=>“btn btn成功”
)
));
如果您只需要一个图标,那么使用css是一个更简单的选项,在表单文件中,您只需将自定义css类添加到按钮,然后在样式表中使用before和以下内容将图标添加到类中:
$this->add(array(
'type' => 'Button',
'name' => 'submit',
'options' => array(
'label' => 'Modifica',
),
'attributes' => array(
'type' => 'submit',
'class' => 'btn btn-warning custom-btn-with-icon'
)
));
然后在css中:
.custom-btn-with-icon:before {
content: '\uxf005'; // this is the unicode of the custom-char you need for your icon
font-family: 'fontAwesome'; // and this is the icon font of your project
}
你必须手动呈现包含html内容的按钮。如果我没有完全弄错的话,
Zend\Form
不可能包含html内容,因为出于安全原因,它将在内部转义。