ZF2表单操作

1.Date

//http://my.oschina.net/cart/
$this->add(array(
	            'type' => 'Zend\Form\Element\Date',
	            'name' => 'birth',
	            'options' => array(
	                'label' => 'Birth'
	            )
	        ));
2.Email
$this->add(array(
            'type' => 'Zend\Form\Element\Date',
            'name' => 'birth',
            'options' => array(
                'label' => 'Birth'
            )
        ));
3.hidden
$this->add(array(
            'name' => 'id',
            'attributes' => array(
                'type'  => 'hidden',
            ),
        ));
4.MultiCheckbox
$this->add(array(
            'type' => 'Zend\Form\Element\MultiCheckbox',
            'name' => 'hobby',
            'options' => array(
                'label' => 'Please choose one/more of the hobbies',
                'value_options' => array(
                    '1' =>'Cooking',
                    '2'=>'Writing',
                    '3'=>'Others'
                ),
            ),
            'attributes' => array(
                'value' => '1' //set checked to '1'
            )
        ));
5.Radio
$this->add(array(
            'type' => 'Zend\Form\Element\Radio',
            'name' => 'direction',
            'options' => array(
                'label' => 'Please choose one of the directions',
                'value_options' => array(
                    '1' => 'Programming',
                    '2' => 'Design',
                ),
            ),
            'attributes' => array(
                'value' => '1' //set checked to '1'
            )
        ));
6.Select
$this->add(array(
            'type' => 'Zend\Form\Element\Select',
            'name' => 'gender',
            'options' => array(
                'label' => 'Gender',
                'value_options' => array(
                    '1' => 'Select your gender',
                    '2' => 'Female',
                    '3' => 'Male'
                ),
            ),
            'attributes' => array(
                'value' => '1' //set selected to '1'
            )
        ));
7.text
$this->add(array(
            'name' => 'name',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Name',
            ),
        ));
8.模板中更改表单错误提示HTML代码
$this->formElementErrors()
->setMessageOpenFormat('<div%s><span>')
->setMessageCloseString('</span></div>')
->setMessageSeparatorString('</span><span>')
->setAttributes(array('class' => 'error'));
如果需要ZF2开发中常用操作,请访问

ZF2开发中常用操作

你可能感兴趣的:(ZF2表单)