zend-frameworkzend-formzend-form-elementzend-form-select

Zend Select Element not working


I have the following Zend_Debug value for Albums and Category in my IndexController:

    array(4) {
  ["id"] => string(1) "1"
  ["artist"] => string(18) "Paula Abdul Rashid"
  ["title"] => string(13) "Sunny Side Up"
  ["category_id"] => NULL
}

array(10) {
  [0] => array(2) {
    ["id"] => string(1) "1"
    ["name"] => string(7) "Country"
  }
  [1] => array(2) {
    ["id"] => string(1) "2"
    ["name"] => string(7) "Hip Hop"
  }
  [2] => array(2) {
    ["id"] => string(1) "3"
    ["name"] => string(4) "Jazz"
  }
  [3] => array(2) {
    ["id"] => string(1) "4"
    ["name"] => string(14) "Latin American"
  }
  [4] => array(2) {
    ["id"] => string(1) "5"
    ["name"] => string(3) "Pop"
  }
  [5] => array(2) {
    ["id"] => string(1) "6"
    ["name"] => string(3) "R&B"
  }
  [6] => array(2) {
    ["id"] => string(1) "7"
    ["name"] => string(4) "Rock"
  }
  [7] => array(2) {
    ["id"] => string(1) "8"
    ["name"] => string(3) "Ska"
  }
  [8] => array(2) {
    ["id"] => string(1) "9"
    ["name"] => string(5) "Asian"
  }
  [9] => array(2) {
    ["id"] => string(2) "10"
    ["name"] => string(11) "Modern folk"
  }
}

And in my Form_Albums I have the following form elements :

$id = new Zend_Form_Element_Hidden('id');
        $id->addFilter('Int');

        $artist = new Zend_Form_Element_Text('artist');
        $artist->setLabel('Artist')
               ->setRequired(true)
               ->addFilter('StripTags')
               ->addFilter('StringTrim')
               ->addValidator('NotEmpty');

        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Title')
              ->setRequired(true)
              ->addFilter('StripTags')
              ->addFilter('StringTrim')
              ->addValidator('NotEmpty');

        $category = new Zend_Form_Element_Select('category');
        $category->setLabel('Category')
                ->setRequired(true);


        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton');



        $this->addElements(array($id, $artist, $title, $category, $submit));

I couldn't get the dropdownlist display using the Zend_Form_Element_Select..

Update for Solutions:

I got it working now by using the following:

$category = new Application_Model_DbTable_Category();
$category_list = $category->findForSelect();


foreach ($category_list as $cat) {     //$data is your array variable
    Zend_Debug::dump($cat['id']." :: ". $cat['name']);
    $form->category->addMultiOption($cat['id'], $cat['name']);      
}

Thanks to Rishi for opening the idea.


Solution

  • Something like this in your controller might help you out..

    $form = new Form_Albums();
    foreach ($data as $c) {     //$data is your array variable
    $form->category->addMultiOption($c->id, $c->name);      
    }