ZF2 Select element usage

那年仲夏 提交于 2020-01-17 04:48:04

问题


I'm using Zend Framework 2 and I need a Dependent Dropdown. When user select an category (cat_id on my example) the system fills the subcategory (sca_id) with the correct elements.

I could do that by creating an application like this:

My form looks like:

    $this->add(array(
        'name' => 'cat_id',
        'type' => 'Zend\Form\Element\Select',
        'options' => array(
            'label' => 'Categoria',
            'value_options' => array(
                '' => '',
            ),
        ),
    ));
    $this->add(array(
        'name' => 'sca_id',
        'type' => 'Zend\Form\Element\Select',
        'options' => array(
            'label' => 'Sub Categoria',
            'style' => 'display:none;', // Esse campo soh eh exibido qndo uma categoria for escolhida
            'value_options' => array(
                '' => '',
            ),
        ),
    ));

Note that I don't fill the value_options there, because I choose do that in my controller, where the Service Manager is avaliable:

    $form = new ProdutoForm('frm');
    $form->setAttribute('action', $this->url()->fromRoute('catalogo-admin', array( ... )));
    // Alimenta as comboboxes...
    $form->get('cat_id')->setValueOptions($this->getCategoriaService()->listarCategoriasSelect());

On the change event of cat_id I do an $.ajax to grab the elements from an Action and fill the sca_id.

That works fine!

The problem is on my validation:

    $this->add(array(
        'name' => 'cat_id',
        'require' => true,
        'filters'  => array(
            array('name' => 'Int'),
        ),
    ));
    $this->add(array(
        'name' => 'sca_id',
        'require' => true,
        'filters'  => array(
            array('name' => 'Int'),
        ),
    ));

When I submit my form it keeps saying : The input was not found in the haystack for both dropdowns...

What I'm doing wrong?

Extra questions : There's a better way to fill my dropdowns?

Ps.: I guess this question Disable notInArray Validator Zend Framework 2 asks something similar than me, but I wanted to detail more my problem.


回答1:


Well, I realized that I should populate my select element before validate my form!

// SaveAction
$request = $this->getRequest();
if ($request->isPost())
{
    $form = new ProdutoForm();

    // Alimenta as comboboxes...
    $form->get('cat_id')->setValueOptions($this->getCategoriaService()->listarCategoriasSelect());
    $form->get('sca_id')->setValueOptions($this->getSubCategoriaService()->listarSubCategoriasSelect());

    // If the form doesn't define an input filter by default, inject one.
    $form->setInputFilter(new ProdutoFormFilter());

    // Get the data.
    $form->setData($request->getPost());

    // Validate the form
    if ($form->isValid())
    {
        // Valid!
    }else{
        // Invalid...
    }

That code works nice. My form now validate perfectly!



来源:https://stackoverflow.com/questions/13181799/zf2-select-element-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!