当前位置: 动力学知识库 > 问答 > 编程问答 >

html select - Zend Framework 2: Trying to add a selectbox to a form does not render values

问题描述:

I am trying to add a selectbox to one of my forms (which just with input type="text" elements are working pretty good) but all I get is just an empty selectbox with none tags in it. So this is the code I use:

Bla.php :: Bla->getInputFilter()

$inputFilter->add($factory->createInput(array(

'type' => 'Zend\InputFilter\Select',

'name' => 'payment_type',

'required' => true,

'filters' => array(

array('name' => 'Int'),

),

)));

BlaForm.php :: BlaForm->__construct():

$this->add(array(

'type' => 'Zend\Form\Element\Select',

'name' => 'payment_type',

'options' => array(

'label' => 'Payment',

'value_options' => array(

0 => 'Nur Überweisung',

1 => 'Nur Paypal',

2 => 'Nur Barzahlung im Voraus',

),

),

'attributes' => array(

'value' => 0 //set selected to "Nur Überweisung"

)

));

bla.php (View)

<div class="control-group">

<?php

echo $this->formLabel($form->get('payment_type')->setLabelAttributes(array(

'class' => 'control-label'

)));

?>

<div class="controls">

<?=$this->formElement($form->get('payment_type'));?>

<span class="help-inline"><?=$this->formElementErrors($form->get('payment_type'));?></span>

</div>

</div>

I already tried using "options" instead of "value_options" and yesterday I learned that it is just an alias of "value_options". Also I tried formSelect() instead of formElement() in my view but that doesn't change anything either. I even removed the umlauts from the strings for testing purposes...

Did anybody experience the same problem or has any idea, what I am currently doing wrong?

网友答案:

I just tried your examples locally against current master (rev 9747bd01d), and they worked without issue -- using either formCollection() on the form, or formElement() or formSelect() on the individual element. In each case, I get the following markup:

<select name="payment_type"><option value="0" selected="selected">Nur Überweisung</option>
<option value="1">Nur Paypal</option>
<option value="2">Nur Barzahlung im Voraus</option></select>

What version of ZF2 are you using? Can you test against either 2.0.2 or current master, please?

网友答案:

I found the solution myself. In BlaForm.php the format of the selectbox element has to be as follows:

$this->add(array(
    'type' => 'Zend\Form\Element\Select',
    'name' => 'payment_type',
    'options' => array(
        'label' => 'Bezahlung',
    ),
    'attributes' => array(
        'options' => array(
            0 => 'Nurerweisung',
            1 => 'NurPaypal',
            2 => 'NurBarzahlung im Voraus',
            3 => 'NurBarzahlung am Bus',
        ),
        'value' => 2 //set selected to "public"
    )
));

The "options" and "value" need to be nested unter "attributes"... well yeah, why not? I found out by looking deeper inter Zend\Form\Element\Select where a method "getOptionAttributeValues()" exists, which gave me the hint.

分享给朋友:
您可能感兴趣的文章:
随机阅读: