Zend framework 在zend framework中创建具有相同选项集的多个元素。

Zend framework 在zend framework中创建具有相同选项集的多个元素。,zend-framework,zend-form-element,Zend Framework,Zend Form Element,嗨,我必须用相同的选项在zend中创建一些multiselect元素。i、 e $B1 = new Zend_Form_Element_Multiselect('Rating'); $B1->setLabel('B1Rating') ->setMultiOptions( array( 'NULL' => "Select", '1' => '1', '2

嗨,我必须用相同的选项在zend中创建一些multiselect元素。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);

现在我必须创建5个相同类型但不同标签的元素。所以我不想把整个代码复制5次。所以,有没有任何方法,我可以这样做,而不复制粘贴代码5次

我想到了三种不同的方式。这是最简单的一个

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

我想到了三种不同的方式。这是最简单的一个

$B2 = clone $B1;
$B2->setLabel('B2Rating');
另一种方法:

$options = array(
    'required'     => true,
    'validators'   => array('NotEmpty'),
    'value'        => null,
    'size'         => 5,
    'multiOptions' => array(
              'NULL' => "Select", 
              '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'),
);

$B1 = new Zend_Form_Element_Multiselect('Rating', $options);
$B1->setLabel('B1Rating')
$this->addElement($B1);

$B2 = new Zend_Form_Element_Multiselect('Rating2', $options);
$B2->setLabel('B2Rating')
$this->addElement($B1);
等等…

另一种方法:

$options = array(
    'required'     => true,
    'validators'   => array('NotEmpty'),
    'value'        => null,
    'size'         => 5,
    'multiOptions' => array(
              'NULL' => "Select", 
              '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'),
);

$B1 = new Zend_Form_Element_Multiselect('Rating', $options);
$B1->setLabel('B1Rating')
$this->addElement($B1);

$B2 = new Zend_Form_Element_Multiselect('Rating2', $options);
$B2->setLabel('B2Rating')
$this->addElement($B1);

诸如此类……

因为实现某个目标的方式从来没有限制,这里有另一个解决方案:

$ratingLabels = array('Rating 1', 'Rating 2', 'Rating 3');

foreach($ratingLabels as $index => $ratingLabel) {
    $this->addElement('multiselect', 'rating' . (++$index), array(
        'required' => true,
        'label' => $ratingLabel,
        'value' => 'NULL',
        'size' => 5,
        'multiOptions' => array(
            'NULL' => 'Select',
            '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'
        ),
    ));
}

因为实现某个目标的方式从来没有限制,所以这里有另一个解决方案:

$ratingLabels = array('Rating 1', 'Rating 2', 'Rating 3');

foreach($ratingLabels as $index => $ratingLabel) {
    $this->addElement('multiselect', 'rating' . (++$index), array(
        'required' => true,
        'label' => $ratingLabel,
        'value' => 'NULL',
        'size' => 5,
        'multiOptions' => array(
            'NULL' => 'Select',
            '1' => '1', '2' => '2', '3' => '3', '4' => '4', '5' => '5'
        ),
    ));
}

看看这些评论。嘿,谢谢。实际上,从浅层或深层克隆的角度来看,我还发现我们可以使用类似$form=new Zend_form_Element_Multiselect()$新表单=$form->deepclone()@太好了,我不知道。看看评论。嘿,谢谢。实际上,从浅层或深层克隆的角度来看,我还发现我们可以使用类似$form=new Zend_form_Element_Multiselect()$新表单=$form->deepclone()@太好了,我不知道。