Search code examples
zend-frameworkzend-form-element

creating multiple elements with same set of options in zend framework.


Hi i have to create some multiselect element in zend with same options. i.e.

$B1 = new Zend_Form_Element_Multiselect('Rating');
$B1->setLabel('B1Rating')
          ->setMultiOptions(
              array(
                  'NULL' => "Select", 
                  '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'))
          ->setRequired(TRUE)
          ->addValidator('NotEmpty', true);
    $B1->setValue(array('NULL'));
    $B1->size = 5;    
    $this->addElement($B1);

Now i have to create 5 elements of same type but different labels. So i don't want to copy the whole code 5 times. So is there any way i can do so without copy-pasting the code for 5 times.


Solution

  • About three different ways spring to mind. Here's the simplest one

    $B2 = clone $B1;
    $B2->setLabel('B2Rating');