在zf2中动态填充表单

时间:2013-12-10 09:05:48

标签: forms frameworks zend-framework2

我需要在生成表单时在ZF2中填充我的数据库中的“select”选项(不使用Doctrine)。 我的表格:

class RegisterTeacherForm extends Form
{
   ....

    $this->add(array(
        'type' => 'Zend\Form\Element\Select',
        'name' => 'RegisterTeacherCountry',
        'attributes' => array(
            'id' => 'country_id',  
            'class' => 'selectable',
            'autocomplete' => 'off',
        ),
        'options' => array(
            'value_options' => array(
                'default' => 'Please Select',
                'Austria' => 'Austria',  //how i do fill this section from DB?
                'France' => 'France',
                'Germany' => 'Germany',
                'Spain' => 'Spain',
            ),
        ),
    ));
}

谢谢大家的回答!

1 个答案:

答案 0 :(得分:1)

如果您使用的是学说,请执行以下操作:

$this->add(array(
        'name' => 'RegisterTeacherCountry',
        'type' => 'DoctrineModule\Form\Element\ObjectSelect',
        'attributes' => array(
            'id' => 'country_id',  
            'class' => 'selectable',
            'autocomplete' => 'off',
        ),
        'options' => array(
            'object_manager' => $this->getEntityManager(),
            'target_class' => 'YOUR ENTITY NAMESPACE',
            'property' => 'your db collumn name',
            'disable_inarray_validator' => true,
            'by_reference' => false,
            'is_method'      => true,
            'find_method'    => array(
                'name'   => 'findBy',
                'params' => array(
                    'criteria' => array(),
                    'orderBy'  => array('nameOfCollumnYouWantToOrderBy' => 'ASC'),
                ),
            ),
        ),
    ));