Zend Framework: Populating DB data to a Zend Form dropdown element
Asked Answered
C

3

5

I have the following form:

<?php
class Application_Form_RegistrationForm extends Zend_Form{

    public function init(){

        $country = $this->createElement('select', 'country');
        $country->setLabel('country: ')
                ->setRequired(true);


        $email = $this->createElement('text', 'email_address');
        $email->setLabel('Email Address: ')
                ->setRequired(true);

        $register = $this->createElement('submit', 'register');
        $register->setLabel('Create new Account')
                ->setIgnore(true);

        $this->addElements(array(
            $country, $email, $register
        ));




    }

}

?>

The list of the countries are present in a table country in a database.

Is there anyway I can populate the country dropdown list with the country names from the database?

Any help is appreciated.

Thanks

Copp answered 8/10, 2010 at 12:38 Comment(0)
L
4

The best way is to create a new class for the element:

Put this in "/application/form/element/CountySelect.php"

class Application_Form_Element_CountrySelect extends Zend_Form_Element_Select {
    public function init() {
        $oCountryTb = new Application_Model_Country();
        $this->addMultiOption(0, 'Please select...');
        foreach ($oCountry->fetchAll() as $oCountry) {
            $this->addMultiOption($oCountry['id'], $oCountry['name']);
        }
    }
}

And then add it to the form this way:

class Application_Form_RegistrationForm extends Zend_Form{
    public function init() {
        $this->addElement(new Application_Form_Element_CountrySelect('country_id'));
    }
}
Lifework answered 17/10, 2010 at 20:36 Comment(1)
and then when this doesn't work because there is no explanation for the new Application_Model_Country()...Hedgehop
C
9

You sure can.

In the init method you can set the options with something like this, assuming $db is a Zend_Db adapter:

$options = $db->fetchPairs('SELECT id, name FROM country ORDER BY name ASC');
$country->setMultiOptions($options);

In case you haven't seen the fetchPairs method, it builds an array, where the first column return becomes the key, and the second column the value.

Chamonix answered 11/10, 2010 at 22:26 Comment(0)
S
6

You could do it from controller's action (or even in Service Layer, if to be meticulous), if your list's content depends from some conditions. Usage:

$form->getElement('country')->addMultiOption('1','USA');     //add single value
$form->getElement('country')->addMultiOptions(array('1'=>'USA', '2'=>'Canada'));     //add values by array
$form->getElement('country')->setMultiOptions(array('1'=>'USA', '2'=>'Canada'));     //set values by array

Of course, to add values from DB you need to fetch them first.

See http://framework.zend.com/manual/en/zend.form.standardElements.html#zend.form.standardElements.select for more methods available.

Sillsby answered 14/10, 2010 at 8:17 Comment(1)
+1, Yet, Things changed a little bit in in Zend 2. Thus, your last line will be rewritten as $form->get('country')->setValue(array('1'=>'USA', '2'=>'Canada'));Lowermost
L
4

The best way is to create a new class for the element:

Put this in "/application/form/element/CountySelect.php"

class Application_Form_Element_CountrySelect extends Zend_Form_Element_Select {
    public function init() {
        $oCountryTb = new Application_Model_Country();
        $this->addMultiOption(0, 'Please select...');
        foreach ($oCountry->fetchAll() as $oCountry) {
            $this->addMultiOption($oCountry['id'], $oCountry['name']);
        }
    }
}

And then add it to the form this way:

class Application_Form_RegistrationForm extends Zend_Form{
    public function init() {
        $this->addElement(new Application_Form_Element_CountrySelect('country_id'));
    }
}
Lifework answered 17/10, 2010 at 20:36 Comment(1)
and then when this doesn't work because there is no explanation for the new Application_Model_Country()...Hedgehop

© 2022 - 2024 — McMap. All rights reserved.