0
votes

I am a newbie to Zend Framework and I am trying to making a Form using Zend Form. In that Form I want cascading drop downs of country, state and city. I have filled country's drop down from database but I have no idea how do I fill state's drop down by getting the country id.

Thanks in advance.

2

2 Answers

0
votes

You have to set an associative array like this :

$tCountries = array(
    1 => "France",
    2 => "USA"
);

$element = new Zend_Form_Element_Select();
$element->addMultiOptions($tCountries);

You could find more informations here : http://framework.zend.com/manual/en/zend.form.standardElements.html#zend.form.standardElements.select

-2
votes

To dynamically load countries you should use it the way you did:

I have done this previously with standard php and jquery

And here is nothing to do with Zend_Form. You should have a JSON service (for e.g.) that will accept CountryID and will return all states available.

For this purpose you should connect jQuery library in your <head> section.

$this->view->headScript()->appendFile('/js/jquery-1.7.3.js'); // In your Controller

Add you jQuery script to your template which will handle all events like this:

$_form->inlineScript()->appendScript(' // my raw JS here');

or

$_form->inlineScript()->appendFile('js/dynamicCountry.js'); // if you want to keep it in separate file

And here you are, nothing to do with Zend_From except assigning correct elements ID's, classes or names.

And for your service you can do smth. similar. Suppose URL http://localhost/mysite/service/states/country/AT

/**
 * In your Service controller disable rendering for your layout and views
 */
public function preDispatch() {
        $this->_helper->layout()->disableLayout(); // if you have `layout` enabled
        $this->_helper->viewRenderer->setNoRender(true);
    }

and ... welcome

public function statesAction() {

    $request = $this->getRequest();

    /**
     * Country code transmitted through the parameter
     * @var string
     */
    $country = $request->getParam('country');

        // you are free to do whatever you want and return JSON (for e.g.)
}