Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 626
pebbo Avatar asked Oct 13 '12 01:10

pebbo


1 Answers

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?

like image 54
weierophinney Avatar answered Oct 12 '22 09:10

weierophinney