Search code examples
zend-frameworkjqueryzend-form

Zend_Form with Ajax/json


I'm a bit lost using Zend_Form with Ajax. I have a form in a class extending Zend_Form called from my controller, that way :

GoodAddGroup.php

class Default_Form_GoodAddGroup extends Zend_Form {
    (...)

    public function init()
    {
        $this->setMethod('post');
        $this->setAction("process-add-group");
        $this->setName("addgroupgood");

        // Load Elements class
        require "Form/Elements.php";
        $magElements = new Elements();

        // Category
        $categoryElement = $magElements->getCategorySelectField();
        $categoryElement->setDecorators($this->elementDecorators);

        // Barcode
        $barcodeElement = $magElements->getGoodBarcodeTextField();
        $barcodeElement->setDecorators($this->elementDecorators);

        (...)

        // Add elements to the form
        $this->addElements(array(
            $categoryElement,
            //$codeElement,
            $barcodeElement,
            $serialElement,
            $warehouseElement,
            $submitButtonElement
        ));
        $this->setDecorators($this->formDecorators);
    }
}

In GoodsController.php

private function getAddGroupForm()
{
    return new Default_Form_GoodAddGroup();
}

public function addGroupAction()
{
    // Initialize the form for the view.
    $this->view->form = $this->getAddGroupForm();
}

public function processAddGroupAction()
{

        $form = $this->getAddGroupForm();
    (...)

    if ($_POST)
    {
        if ($form->isValid($_POST))
        {
            // Do things
        } else {
            $this->view->form = $form;
        }
    }
}

Basically, the form has a category select field, when selecting a category, a second "code" selector is added filled with the items related to this category. When the page with the form is displayed (http://myapp/goods/add-group), everything works fine, the ajax call does its job, the second select field is added and well fed, but as you can see, the form processing is done with the processAddGroupAction(), this method get the instance of the form to get its values and to re-display it in case of problem. But that way, my "new" select field doesn't exist anymore, so i can never validate the form.

It's my first attempt using ajax/json with Zend, i think i need help at this poind.

Thank you.

EDIT : added the view code as requested

<script>
    $(function(){
        $("#cats").change(function(){
            getSelectBox(this);
        });

        $("#code").parent().parent().css('display', 'none');
        getSelectBox($("#cats"));
    });

    function getSelectBox(element)
    {
        if($(element).val() != '')
        {
            $("#code").parent().parent().css('display', 'block');
            if ($('#code').length <= 0) {
                $("#cats").after('<select name="code" id="code" style="margin-left:10px"></select>');
            }
            $.getJSON("/goods/json-get-codes-from-category", {id: $(element).val(), ajax: "true"}, function(j){
                console.log(j);
                var options = "";
                jQuery.each(j, function(i, val) {
                    options += '<option value="' + i + '">' + i + val + '</option>';
                });
                $("#code").html(options);
            });
        }
    }
</script>

<?php echo $this->form; ?>

Solution

  • Ok, i found the solution! I post it in case it can be useful for other people. I knew what was the problem, but unable to know how to achieve it.

    At form processing time, the new select element does not exist for the action as it has been added in the view with javascript. To fix this, we need to inform the action about the new field, to do so, we first need to override the method “isValid” of Zend_Form in the form class :

    public function isValid($values)
    {
        $values = $this->_modifyElements($values);   
        return parent::isValid($values);
    }
    

    Then create a method "_modifyElements" that will modify the form by adding the new element :

    protected function _modifyElements($values)
        {
            // Search for codes
            $dbu = new DbUtils();
            $codes = $dbu->getCodesFromCategory($values['cats']);
    
            // Remove the current code element
            $this->removeElement('code');
    
            // Create a new element
            $codeElement = new Zend_Form_Element_Select('code', array());
            $codeElement->setLabel(_('Code :'));
            $codeElement->setRequired(true);
            $codeElement->addMultiOptions($codes);
            $codeElement->setValue($values['code']);
            $codeElement->setDecorators($this->elementDecorators);
            $this->addElement($codeElement);
    
            return $values;
        }
    

    We have to override the method "populate" too :

    public function populate(array $values)
    {
        $values = $this->_modifyElements($values);   
        return parent::populate($values);
    }
    

    And voilà. It works for me ;> All the credits about this go to Mohamed : http://jamandcheese-on-phptoast.com/2009/12/13/on-fly-elements-in-zend_form/