[英]How to add class and id to form element in Zend Framework 2.2.4
我需要的HTML:
<label for="text_field_username">User Name</lable>
<input type="text" id="text_field_username" name="text_field_username" class="form-control" />
我希望標簽的for鏈接到輸入的id。 這樣,用戶可以單擊標簽以突出顯示輸入。 對於復選框更有用。 另外,不太重要,我希望在輸入字段中有一個類。
我嘗試過但對我不起作用:
echo $this->formRow($form->get('usr_name'));
我也嘗試使用局部布局。
echo $this->formElement($element);
在發布此問題之前,我遇到了這個文檔framework.zend.com/manual/2.2/en/modules/zend.form.view.helpers.html#formlabel
它不起作用。 它添加了for但它沒有任何意義。 !?
查看partials有助於呈現表單,但它們不會處理表單元素本身的屬性。 這由表單類及其表單元素集合(即TextElement)處理。
您可以在任何表單元素上使用setAttribute('class', 'class name')
因此,在表單的init()
方法中,這應該工作:
$element = $this->getElement('text_field_username');
$element->setAttribute('class', 'class name');
你也可以在你繼承的表單助手類中設置它,如下所示:
namespace Application\Form;
use Zend\Form\Form;
class NexForm extends Form
{
public function __construct($name = null)
{
parent::__construct('Nex');
$this->setAttribute('method', 'post');
$this->setAttribute(
'enctype',
'multipart/form- data'
);
$this->add(array(
'name' => 'default_widget',
'attributes' => array(
'type' => 'text',
'id' => 'default_widget',
'class' => 'mtz-monthpicker-widgetcontainer',
'required' => 'required',
),
'options' => array(
'label' => 'Please choose the month of records you want to display:',
),
));
}
}
並在您的視圖中致電:
$nex=$this->app; //app is the form object we passed from controller to view
echo $this->formElement($nex->get('default_widget'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.