[英]Zend Form - Add a <div> around the input element
我正在使用Zend Framework 2 Form來渲染我的表單,而使用Bootstrap 3來設置它們的樣式。 我要使用“水平表單布局”,如此處所述: Bootstrap 3表單教程
為此,我需要在input元素周圍添加一個具有適當類的div; 有什么簡單的方法可以在將新元素添加到Form類時添加此類div? 到目前為止我的元素:
$this->add(array(
'name' => 'name',
'attributes' => array(
'type' => 'text',
'id' => 'contactName',
'maxlength' => '70',
'placeholder' => 'Your full name.',
'class' => 'form-control'
),
'options' => array(
'label' => 'Name: ',
'label_attributes' => array(
'class' => 'control-label col-xs-12 col-sm-2'
)
),
));
我可以在這里添加任何內容,以便在使用<?php echo $this->formRow($this->form->get('name')); ?>
渲染時,在輸入元素周圍輕松添加<div class="col-xs-12 col-sm-10"></div>
<?php echo $this->formRow($this->form->get('name')); ?>
<?php echo $this->formRow($this->form->get('name')); ?>
?
我終於通過創建自定義View Helper來解決問題:
public function __invoke($element) {
echo '<div class="form-group">';
echo $this->view->formLabel($element);
echo '<div class="col-xs-12 col-sm-10">' . $this->view->formElement($element) . '</div>';
echo $this->view->formElementErrors($element);
echo '</div>';
}
雖然我仍然想知道是否有另一種更簡單的方法來做到這一點!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.