Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8880127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:06:23+00:00 2026-06-14T20:06:23+00:00

I’m a bit lost using Zend_Form with Ajax. I have a form in a

  • 0

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; ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:06:24+00:00Added an answer on June 14, 2026 at 8:06 pm

    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/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.