Zend framework 使用zend decorator在表格列中格式化zend_Form_Element_Radio,并在行中使用其他zend_Form_元素

Zend framework 使用zend decorator在表格列中格式化zend_Form_Element_Radio,并在行中使用其他zend_Form_元素,zend-framework,zend-form,zend-decorators,Zend Framework,Zend Form,Zend Decorators,我希望使用decorators将以下Zend_表单格式化为表,在第一列中放置一个描述,在第二列中放置Zend_表单元素\u Radio的选项,并在每行中添加2个select,如稍后的html示例所示 我需要一个具体/有效的例子。 形式 预期的HTML 友善的 喝 食物 说明第1行 水 牛奶 啤酒 请选择 汉堡 果 肉 披萨 蔬菜 沃尔斯特 说明第2行 水 牛奶 啤酒 请选择 汉堡 果 肉 披萨 蔬菜 沃尔斯特 说明第3行 水 牛奶 啤酒 请选择 汉堡 果 肉 披萨 蔬菜 沃尔斯特 说明第4行

我希望使用decorators将以下Zend_表单格式化为表,在第一列中放置一个描述,在第二列中放置Zend_表单元素\u Radio的选项,并在每行中添加2个select,如稍后的html示例所示

我需要一个具体/有效的例子。

形式

预期的HTML


友善的
喝
食物
说明第1行
水
牛奶
啤酒
请选择
汉堡
果
肉
披萨
蔬菜
沃尔斯特
说明第2行
水
牛奶
啤酒
请选择
汉堡
果
肉
披萨
蔬菜
沃尔斯特
说明第3行
水
牛奶
啤酒
请选择
汉堡
果
肉
披萨
蔬菜
沃尔斯特
说明第4行
水
牛奶
啤酒
请选择
汉堡
果
肉
披萨
蔬菜
沃尔斯特
说明第5行
水
牛奶
啤酒
请选择
汉堡
果
肉
披萨
蔬菜
沃尔斯特
说明第6行
水
牛奶
啤酒
请选择
汉堡
果
肉
披萨
蔬菜
沃尔斯特

在我看来,你不可能用视图助手完成你想完成的事情

您应该进行自定义表单打印,例如:

<form action="/" method="POST">
    <table>
        <thead>
        <tr>
            <th></th>
            <th>kind</th>
            <th>drink</th>
            <th>food</th>
        </tr>
        </thead>
        <tbody>
            <?for ($i = 1; $i <= 6; $i++) : ?>
                <?
                $drink = 'drink_' . $i;
                $food = 'food_' . $i;
                ?>
                <tr>
                    <td>Description row <?=$i?></td>
                    <td>
                        <?=$this->form->$drink?>
                    </td>
                    <td>
                        <?=$this->form->$food?>
                    </td>
                </tr>
            <? endfor;?>
        </tbody>
    </table>
</form>

友善的
喝
食物
描述行

午餐
对于现场,不可能按要求使用,你应该考虑另一种策略。此刻我能想到的

这可能不是完美的解决方案,但它可能会帮助您

class My_Form extends Zend_Form
{
    const KIND_1 = 'dineer1';
    const KIND_2 = 'dineer2';
    const KIND_3 = 'dineer3';
    const KIND_4 = 'dineer4';
    const KIND_5 = 'dineer5';
    const KIND_6 = 'dineer6';

    public static $KINDS = array(
        1 => self::KIND_1,
        2 => self::KIND_2,
        3 => self::KIND_3,
        4 => self::KIND_4,
        5 => self::KIND_5,
        6 => self::KIND_6,
    );

    const DRINK_C = 'c';
    const DRINK_M = 'm';
    const DRINK_W = 'w';

    public static $DRINKS = array(
        self::DRINK_C => "cole",
        self::DRINK_M => "milk",
        self::DRINK_W => "water",
    );

    const FOOD_B = 'b';
    const FOOD_F = 'f';
    const FOOD_M = 'm';
    const FOOD_P = 'p';
    const FOOD_V = 'v';
    const FOOD_W = 'w';

    public static $FOODS = array(
        self::FOOD_B => "burger",
        self::FOOD_F => "fruit",
        self::FOOD_M => "Meat",
        self::FOOD_P => "pizza",
        self::FOOD_V => "vegetables",
        self::FOOD_W => "Wursterl",
    );

    public function init()
    {
        $_please_select = array("" => " please select ");

        $this->setMethod(Zend_Form::METHOD_POST);
        $this->setDisableLoadDefaultDecorators(true);

        $countRows = count(self::$KINDS)+1;
        foreach (self::$KINDS as $k => $_descriprion) {

            $rowForm = new Zend_Form_SubForm();

            $input_lunch = new Zend_Form_Element_Radio('lunch',
                array('disableLoadDefaultDecorators' => true, 
                'label' => 'kind', 
                'label_placement' => 'prepend'));
            $input_lunch ->setMultiOptions(self::$KINDS) ;
            $input_lunch->addDecorators(array(
                array('ViewHelper'),
                array('Label', 
                array('style' => 'display:block;font-weight:bold', // just for this example
                    'placement'=>'PREPEND',
                    'tag'=>'span', //if you want to use other tag
                    'disableFor'=>true) 
                ),
                array(array('data' => 'HtmlTag'), array('tag' => 'td', 'rowspan' => $countRows )),
            ));
            $this->addElement($input_lunch);

            // add label just for the first element
            $drinkLabel = array('disableLoadDefaultDecorators' =>true);
            $drinkDecorators = array(
                array('ViewHelper'),
                array(array('data' => 'HtmlTag'), array('tag' => 'td')),
            );
            if ($k == 1) {
                $drinkLabel['label'] = 'drink';
                $drinkDecorators = array(
                    array('ViewHelper'),
                    array('Label', 
                    array('style' => 'display:block;font-weight:bold',
                        'placement'=>'PREPEND',
                        'tag'=>'span', //if you want to use other tag
                        'disableFor'=>true) 
                    ),
                    array(array('data' => 'HtmlTag'), array('tag' => 'td')),
                );
            }

            $input_drink = new Zend_Form_Element_Select('drink', $drinkLabel);
            $input_drink->addMultiOptions(self::$DRINKS);
            $input_drink->addDecorators($drinkDecorators);

            $input_food = new Zend_Form_Element_Select('food', $drinkLabel);
            $input_food->addMultiOptions($_please_select)
                      ->addMultiOptions(self::$FOODS);
            $input_food->addDecorators($drinkDecorators);

            $rowForm->addElement($input_drink);
            $rowForm->addElement($input_food);

            $rowForm->setSubFormDecorators(array(
                'FormElements',
                array('HtmlTag', array('tag' => 'tr')),
            ));
            $rowForm->setDisableLoadDefaultDecorators(true);

            $this->addSubForm($rowForm, 'row_' . $k);
        }

        $this->setSubFormDecorators(array(
          'FormElements',
          array('HtmlTag', array('tag' => 'tr')),
        ));

        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'table')),
            'Form'
        ));
    }
}
此外,这里还有一些使用table和Zend_表单的资源


好了,这是我的答案

init中,我在表单中添加了一个自定义的decoratorRadioTable,并为加载decorator添加了前缀路径

然后我把所有的元素都装饰成一张普通的桌子,这很重要,因为我所有的努力都是为了保持标准装饰师提供的设施,比如“错误”

请注意无线电元素中的自定义表单元素\u first select->setSeparator

我还添加了提交按钮

自定义表单元素\u FirstSelect

自定义表单装饰器无线表格

class Custom\u Form\u Decorator\u RadioTable扩展了Zend\u Form\u Decorator\u Abstract{
公共功能渲染($content){
$wrap='';
//我要元素
$radioElement=$this->getElement()->getElement(“午餐”);
//然后我渲染它并使用分隔符在数组中分解
$arrayRadio=explode(“\t\uuuu RADIO\u SEPARATOR\uuuuu\t”,$radioElement->\uuuu toString());
$count=0;
$arrayElement=$this->getElement()->getElements();
//循环所有表单元素并渲染它们
foreach($arrayElement作为$keyForm=>$element){
//我跳过了无线电元素
if($Zend\u Form\u element\u Radio的元素实例){
继续;
}
if($element instanceof Custom\u Form\u element\u FirstSelect){
//当我找到自定义select元素时,我将在列中以“openrowtag”和单选按钮作为前缀
$wrap.=“”.$arrayRadio[$count++]。“”.$element->;
//请注意,“选择”图元已装饰
}else if($Zend\u Form\u element\u Select的元素实例){
<form action="/" method="POST">
    <table>
        <thead>
        <tr>
            <th></th>
            <th>kind</th>
            <th>drink</th>
            <th>food</th>
        </tr>
        </thead>
        <tbody>
            <?for ($i = 1; $i <= 6; $i++) : ?>
                <?
                $drink = 'drink_' . $i;
                $food = 'food_' . $i;
                ?>
                <tr>
                    <td>Description row <?=$i?></td>
                    <td>
                        <?=$this->form->$drink?>
                    </td>
                    <td>
                        <?=$this->form->$food?>
                    </td>
                </tr>
            <? endfor;?>
        </tbody>
    </table>
</form>
class My_Form extends Zend_Form
{
    const KIND_1 = 'dineer1';
    const KIND_2 = 'dineer2';
    const KIND_3 = 'dineer3';
    const KIND_4 = 'dineer4';
    const KIND_5 = 'dineer5';
    const KIND_6 = 'dineer6';

    public static $KINDS = array(
        1 => self::KIND_1,
        2 => self::KIND_2,
        3 => self::KIND_3,
        4 => self::KIND_4,
        5 => self::KIND_5,
        6 => self::KIND_6,
    );

    const DRINK_C = 'c';
    const DRINK_M = 'm';
    const DRINK_W = 'w';

    public static $DRINKS = array(
        self::DRINK_C => "cole",
        self::DRINK_M => "milk",
        self::DRINK_W => "water",
    );

    const FOOD_B = 'b';
    const FOOD_F = 'f';
    const FOOD_M = 'm';
    const FOOD_P = 'p';
    const FOOD_V = 'v';
    const FOOD_W = 'w';

    public static $FOODS = array(
        self::FOOD_B => "burger",
        self::FOOD_F => "fruit",
        self::FOOD_M => "Meat",
        self::FOOD_P => "pizza",
        self::FOOD_V => "vegetables",
        self::FOOD_W => "Wursterl",
    );

    public function init()
    {
        $_please_select = array("" => " please select ");

        $this->setMethod(Zend_Form::METHOD_POST);
        $this->setDisableLoadDefaultDecorators(true);

        $countRows = count(self::$KINDS)+1;
        foreach (self::$KINDS as $k => $_descriprion) {

            $rowForm = new Zend_Form_SubForm();

            $input_lunch = new Zend_Form_Element_Radio('lunch',
                array('disableLoadDefaultDecorators' => true, 
                'label' => 'kind', 
                'label_placement' => 'prepend'));
            $input_lunch ->setMultiOptions(self::$KINDS) ;
            $input_lunch->addDecorators(array(
                array('ViewHelper'),
                array('Label', 
                array('style' => 'display:block;font-weight:bold', // just for this example
                    'placement'=>'PREPEND',
                    'tag'=>'span', //if you want to use other tag
                    'disableFor'=>true) 
                ),
                array(array('data' => 'HtmlTag'), array('tag' => 'td', 'rowspan' => $countRows )),
            ));
            $this->addElement($input_lunch);

            // add label just for the first element
            $drinkLabel = array('disableLoadDefaultDecorators' =>true);
            $drinkDecorators = array(
                array('ViewHelper'),
                array(array('data' => 'HtmlTag'), array('tag' => 'td')),
            );
            if ($k == 1) {
                $drinkLabel['label'] = 'drink';
                $drinkDecorators = array(
                    array('ViewHelper'),
                    array('Label', 
                    array('style' => 'display:block;font-weight:bold',
                        'placement'=>'PREPEND',
                        'tag'=>'span', //if you want to use other tag
                        'disableFor'=>true) 
                    ),
                    array(array('data' => 'HtmlTag'), array('tag' => 'td')),
                );
            }

            $input_drink = new Zend_Form_Element_Select('drink', $drinkLabel);
            $input_drink->addMultiOptions(self::$DRINKS);
            $input_drink->addDecorators($drinkDecorators);

            $input_food = new Zend_Form_Element_Select('food', $drinkLabel);
            $input_food->addMultiOptions($_please_select)
                      ->addMultiOptions(self::$FOODS);
            $input_food->addDecorators($drinkDecorators);

            $rowForm->addElement($input_drink);
            $rowForm->addElement($input_food);

            $rowForm->setSubFormDecorators(array(
                'FormElements',
                array('HtmlTag', array('tag' => 'tr')),
            ));
            $rowForm->setDisableLoadDefaultDecorators(true);

            $this->addSubForm($rowForm, 'row_' . $k);
        }

        $this->setSubFormDecorators(array(
          'FormElements',
          array('HtmlTag', array('tag' => 'tr')),
        ));

        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'table')),
            'Form'
        ));
    }
}
public function init()
{
    $_please_select = array("" => " please select ");

    // I add a my custom decorator
    $this->addPrefixPath('Custom_Form_Decorator',
                             'Custom/Form/Decorator',
                             'decorator');

    $this->setMethod(Zend_Form::METHOD_POST);

    $input_lunch = new Zend_Form_Element_Radio('lunch');  
    $input_lunch ->setMultiOptions(self::$KINDS)
                 ->setSeparator("\t__RADIO_SEPARATOR__\t")  //set custom separator I'll use in custom decorator
                 ->setDecorators(array(
                     'ViewHelper',  // add a standard decorator
                     'Label', // I'll use the label decorator to show text
                   ));

    $this->addElement($input_lunch );

    foreach (self::$KINDS as $k => $_description) {
        // to "mark" the first select I extend the Zend_Form_Element_Select
        // with Custom_Form_Element_FirstSelect
        $input_drink = new Custom_Form_Element_FirstSelect('drink_' . $k);
        $input_drink->addMultiOptions(self::$DRINKS)                    
                    ->setDecorators(array(
                                    'ViewHelper', // add a standard decorator
                                    array(array('data' => 'HtmlTag'),array('tag' => 'td')), // add a standard decorator
                                        ));

        $input_food = new Zend_Form_Element_Select('food_' . $k);
        $input_food->addMultiOptions($_please_select)
            ->addMultiOptions(self::$FOODS)
            ->setDecorators(array(
                            'ViewHelper',
                            array(array('data' => 'HtmlTag'),array('tag' => 'td')),
                            ));

        $this->addElement($input_drink);
        $this->addElement($input_food);
    }

    // add a the submit button
    $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('SUBMIT');
        $submit->setDecorators(array(
                                'ViewHelper',
                                'Errors',
                                array(array('data' => 'HtmlTag'), array('tag' => 'td', )),
                                array(array('row' => 'HtmlTag'), array('tag' => 'tr')),
                            ));
        $this->addElement($submit);

    // add the custom decorators to the whole form
    $this->setDecorators(array(
        'RadioTable',
        array('HtmlTag', array('tag' => 'table', )),
        'Form'
    ));
}
class Custom_Form_Element_FirstSelect extends Zend_Form_Element_Select{

}
class Custom_Form_Decorator_RadioTable extends Zend_Form_Decorator_Abstract {

    public function render($content){

        $wrap = '';
        // I'll take the element
        $radioElement  = $this->getElement()->getElement('lunch');
        // then I render it and explode in array using the separator
        $arrayRadio = explode("\t__RADIO_SEPARATOR__\t", $radioElement->__toString());



        $count = 0;
        $arrayElement  = $this->getElement()->getElements(); 
        // loop on all form elements and I render them
        foreach ($arrayElement as $keyForm => $element){
            // I skip the radio element 
            if($element instanceof Zend_Form_Element_Radio){
                continue;
            }

            if($element instanceof Custom_Form_Element_FirstSelect ){
                // when I found the custom select element I'll prefix with "open-row-tag" and radio button in column
                $wrap .= "<tr>". "<td>". $arrayRadio[$count++] . "</td>" .  $element->__toString();
                // note that the select elements are already decorated
            } else if($element instanceof Zend_Form_Element_Select){
                // after the last select I close the row
                $wrap .= $element->__toString() ."</tr>";

            }

            if($element instanceof Zend_Form_Element_Submit){
                // add the SUBMIT button
                $wrap .=  $element->__toString() ;

            }
        }

        return $content.$wrap;
    }
}
class My_Form extends Zend_Form
{
    const KIND_1 = 'dineer1';
    const KIND_2 = 'dineer2';
    const KIND_3 = 'dineer3';
    const KIND_4 = 'dineer4';
    const KIND_5 = 'dineer5';
    const KIND_6 = 'dineer6';

    public static $KINDS = array(
        1 => self::KIND_1,
        2 => self::KIND_2,
        3 => self::KIND_3,
        4 => self::KIND_4,
        5 => self::KIND_5,
        6 => self::KIND_6,
    );

    const DRINK_C = 'c';
    const DRINK_M = 'm';
    const DRINK_W = 'w';

    public static $DRINKS = array(
        self::DRINK_C => "cole",
        self::DRINK_M => "milk",
        self::DRINK_W => "water",
    );

    const FOOD_B = 'b';
    const FOOD_F = 'f';
    const FOOD_M = 'm';
    const FOOD_P = 'p';
    const FOOD_V = 'v';
    const FOOD_W = 'w';

    public static $FOODS = array(
        self::FOOD_B => "burger",
        self::FOOD_F => "fruit",
        self::FOOD_M => "Meat",
        self::FOOD_P => "pizza",
        self::FOOD_V => "vegetables",
        self::FOOD_W => "Wursterl",
    );

    public function init()
    {
        $this->addDecorators(
            array(
                array('ViewScript', array('viewScript' => 'forms/_form_test.phtml'))
            )
        ); //added as part of answer. Note all default decorators are still available.

        $_please_select = array("" => " please select ");

        $this->setMethod(Zend_Form::METHOD_POST);

        $input_lunch = new Zend_Form_Element_Radio('lunch');  
        $input_lunch ->setMultiOptions(self::$KINDS) ;
        $this->addElement($input_lunch );

        foreach (self::$KINDS as $k => $_descriprion) {
            $input_drink = new Zend_Form_Element_Select('drink_' . $k);
            $input_drink->addMultiOptions(self::$DRINKS);

            $input_food = new Zend_Form_Element_Select('food_' . $k);
            $input_food->addMultiOptions($_please_select)
                ->addMultiOptions(self::$FOODS);

            $this->addElement($input_drink);
            $this->addElement($input_food);
        }
        $this->setElementDecorators(array('ViewHelper'));//added as part of answer
    }
}
<form
    id='contact' 
    action='<?php echo $this->element->getAction(); ?>' 
    method='<?php echo $this->element->getMethod(); ?>'
>
<table>
    <thead>
        <tr>
            <th></td>
            <th>kind</td>
            <th>drink</td>
            <th>food</td>
        </tr>
    </thead>
    <tbody>
        <?php 
        $elements = $this->element->getElements();
        $options = $this->element->lunch->getMultiOptions();
        foreach($options as $key => $option){
            echo "<tr>\n";
            echo "<td>Description row $key</td>\n";
            echo "<td><input type='radio' name='lunch' value='$option'</td>\n";
            echo "<td>{$elements['drink_' . $key]}</td>\n";
            echo "<td>{$elements['food_' . $key]}</td>\n";
            echo "</tr>\n";
        }
        ?>
        </tbody>
        <table>
</form>