zend : populating a checkbox array with $form->populate(); from controller

We have a zend form with text input fields and array of checkboxes, as shown below -

<input class="checkbox_Category" type="checkbox" name="tag[]" value="19"> somename1 <br/>
<input class="checkbox_Category" type="checkbox" name="tag[]" value="20"> somename2 <br/>
<input class="checkbox_Category" type="checkbox" name="tag[]" value="21"> somename3 <br/>

and using a

$formObject->populate($formDataArray);

in the controller to populate data in the whole form. All the text input fields seem to populate fine, but the checkboxes don't. Int the $formDataArray, the data for the checkboxes is in the format

[tag] => Array ( [0] => 20 [1] => 19 )

Along with the other form data like - [firstName] => 'somename' etc. I am not able to figure out the format of the data the form is expecting, in order to get populated with populate();

Answers


hi might be problem with the name you have given as array please change like below

<input class="checkbox_Category" type="checkbox" name="tag" value="19"> somename1 <br/>
<input class="checkbox_Category" type="checkbox" name="tag" value="20"> somename2 <br/>
<input class="checkbox_Category" type="checkbox" name="tag" value="21"> somename3 <br/>

Please let me know if i can help you further


Without actually seeing your Zend_Form code, this is very difficult. However, most often, I've seen people mistake the 'checkbox' element with the 'multiCheckbox' element in Zend Framework. I know - it's a bit confusing - but checkbox is a single checkbox with an on/off value. MultiCheckbox handles multiple values - and I think it's what you are trying to accomplish. Let me show you a quick form that will work and generate your HTML above.

class Application_Form_Test extends Zend_Form
{
    public function init()
    {
        $this->setMethod('post');

        $multiOptions = array(
            19 => 'somename1',
            20 => 'somename2',
            21 => 'somename3'
        );

        $this->addElement('multiCheckbox', 'tag', array(
            'multiOptions'=>$multiOptions
        ));

        $this->addElement('submit', 'submitbutton');
    }
}

Now, if you use something like...

$form->populate($this->getRequest()->getPost());

in your controller, it will populate as expected.

Hope this helps!


Need Your Help

How do I make a multi-use partial view that renders controls with different name values in asp.net mvc 2?

c# asp.net asp.net-mvc-2 partial-views

I have a web application where all the data is in a single table and is therefore contained within a single class object. The structure of the table is something like:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.