ZF2元素“select”和“multicheckbox”无法正确呈现

时间:2014-02-24 14:12:45

标签: select zend-framework2 element

为什么元素“select”和“multicheckbox”无法正确呈现(在html中)? 元素“text”和“checkbox”正确呈现!

示例:

1)表格中的代码:

class Profile extends Form {

    public function __construct($name = null) {

        parent::__construct('page');
        $this->setAttribute('action', 'info');
        $this->setAttribute('method', 'post');        
        $this->setInputFilter($this->getFilters());`

        $this->add(array(     
            'type' => 'Zend\Form\Element\Select',       
            'name' => 'usernames',
            'attributes' =>  array(
               'id' => 'usernames'              
            ),
            'options' => array(
                'label' => 'User Name',
                'options' => array(
                    'test' => 'Hi, Im a test!',
                    'Foo' => 'Bar',
                ),
            ),
        ));

        $this->add(array(
             'type' => 'select',
             'name' => 'language',
             'options' => array(
                     'label' => 'Which is your mother tongue?',
                     'value' => array(
                             '0' => 'French',
                             '1' => 'English',
                             '2' => 'Japanese',
                             '3' => 'Chinese',
                     ),
             )
        ));
    }

2)视图中的代码:

<h1><?php echo $this->translate('Profile') ?></h1>
<?php $form = $this->form; $form->prepare();?>
<?php echo $this->form()->openTag($form) ?>
    <dl class="zend_form">
    <?php foreach ($form as $element): ?>
        <?php if ($element->getLabel() != null): ?>
            <dt><?php echo $this->translate($this->formLabel($element)); ?></dt>
        <?php endif ?>
        <?php if ($element instanceof Zend\Form\Element\Button): ?>
            <dd><?php echo $this->formButton($element) ?></dd>
        <?php elseif ($element instanceof Zend\Form\Element\Captcha): ?>
            <dd><?php echo $this->formCaptcha($element) . $this->translate($this->formElementErrors($element)); ?></dd>
        <?php else: ?>
            <dd><?php echo $this->formInput($element) . $this->translate($this->formElementErrors($element)); ?></dd>
        <?php endif ?>
    <?php endforeach ?>
    </dl>
    <?php if ($this->redirect): ?>
        <input type="hidden" name="redirect" value="<?php echo $this->redirect ?>" />
    <?php endif ?>
    <input type="submit" value="<?php echo $this->translate('Submit'); ?>" />
<?php echo $this->form()->closeTag() ?>

3)渲染后的html代码:

...

<dt><label for="usernames">User Name</label></dt>
<dd><input type="select" name="usernames" id="usernames" value=""></dd>
<dt><label for="language">Which is your mother tongue?</label></dt>
<dd><input type="select" name="language" value=""></dd>

... 这不正确!? ...

<select>
  <option>value 1</option>
  <option>value 2</option>
</select>

...... - 这是正确的)

1 个答案:

答案 0 :(得分:1)

formInput()助手专门用于输出<input>元素,因此ZF正在按照您的要求进行操作。你可能想要更像这样的东西:

<?php foreach ($form as $element): ?>
    <?php if ($element->getLabel() != null): ?>
        <dt><?php echo $this->translate($this->formLabel($element)); ?></dt>
    <?php endif ?>
    <dd><?php echo $this->formElement($element) . $this->translate($this->formElementErrors($element)) ?></dd>
<?php endforeach ?>

formElement()帮助程序将调用适当的帮助程序。