使用 zend-decorator 格式化表列中的 Zend_Form_Element_Radio 和行中的其他 Zend_Form_Elements

发布于 2024-12-19 09:45:14 字数 9177 浏览 3 评论 0原文

我想使用装饰器将以下 Zend_Form 格式化为表格,在第一列中放置描述在第二列中放置 Zend_Form_Element_Radio 的选项添加 2 在每一行中选择,如稍后的 html 示例中所示。

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

表格

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);

        $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);
        }
    }
}

预期的 HTML

<html>
<body>
    <form action="/" method="POST">
    <table>
    <thead>
        <tr>
            <th></td>
            <th>kind</td>
            <th>drink</td>
            <th>food</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Description row 1</td>
            <td><input type="radio" name="lunch" value "dinner1"></td>
            <td>
                <select name="drink_1">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_1">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 2</td>
            <td><input type="radio" name="lunch" value "dinner2"></td>
            <td>
                <select name="drink_2">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_2">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 3</td>
            <td><input type="radio" name="lunch" value "dinner3"></td>
            <td>
                <select name="drink_3">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_3">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 4</td>
            <td><input type="radio" name="lunch" value "dinner4"></td>
            <td>
                <select name="drink_4">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_4">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 5</td>
            <td><input type="radio" name="lunch" value "dinner5"></td>
            <td>
                <select name="drink_5">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_5">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 6</td>
            <td><input type="radio" name="lunch" value "dinner6"></td>
            <td>
                <select name="drink_6">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_6">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        </tbody>
        <table>
    </form>
</body>
</html>

I want use decorators to format as table the following Zend_Form, placing a description in the first column and the Zend_Form_Element_Radio's options in second column and add 2 select in every row as you can see in the html example later.

I need a concrete/working example.

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);

        $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);
        }
    }
}

expected HTML

<html>
<body>
    <form action="/" method="POST">
    <table>
    <thead>
        <tr>
            <th></td>
            <th>kind</td>
            <th>drink</td>
            <th>food</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Description row 1</td>
            <td><input type="radio" name="lunch" value "dinner1"></td>
            <td>
                <select name="drink_1">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_1">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 2</td>
            <td><input type="radio" name="lunch" value "dinner2"></td>
            <td>
                <select name="drink_2">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_2">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 3</td>
            <td><input type="radio" name="lunch" value "dinner3"></td>
            <td>
                <select name="drink_3">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_3">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 4</td>
            <td><input type="radio" name="lunch" value "dinner4"></td>
            <td>
                <select name="drink_4">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_4">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 5</td>
            <td><input type="radio" name="lunch" value "dinner5"></td>
            <td>
                <select name="drink_5">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_5">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Description row 6</td>
            <td><input type="radio" name="lunch" value "dinner6"></td>
            <td>
                <select name="drink_6">
                    <option value="w">Water</option>
                    <option value="m">Milk</option>
                    <option value="b">Beer</option>
                </select>
            </td>
            <td>
                <select name="food_6">
                    <option value="">please select</option>
                    <option value="b">Burger</option>
                    <option value="f">Fruit</option>
                    <option value="m">Meat</option>
                    <option value="p">Pizza</option>
                    <option value="v">Vegetable</option>
                    <option value="w">Wurstel</option>
                </select>
            </td>
        </tr>
        </tbody>
        <table>
    </form>
</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

农村范ル 2024-12-26 09:45:14

我会根据这里的答案建议一个解决方案 https://stackoverflow.com/a/8451723/212940

您的表单: -

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
    }
}

如您所见,它只需要两个小更改。
然后,您需要创建文件 scripts/forms/_form_test.phtml,其中包含:-

<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>

文件 _form_test.phtml 实际上是用于渲染表单的装饰器。这是使用 Zend_Form 的一种非常灵活的方式,我通过阅读这篇文章学习了如何做到这一点< /a>

默认装饰器,例如“error”,应该仍然可以使用此方法。

在我的系统上,我得到了您要求的确切 html 输出。尝试一下看看。

I would suggest a solution based on this answer here https://stackoverflow.com/a/8451723/212940

Your 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()
    {
        $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
    }
}

As you can see it requires only two small changes.
Then you need to create the file scripts/forms/_form_test.phtml which contains:-

<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>

The file _form_test.phtml is effectively your decorator for rendering the form. This is a very flexible way of using Zend_Form and I learnt how to do this by reading this article here

The default decorators, such as 'error' should still be available with this method.

On my system I got the exact html output you asked for. Try it and see.

榕城若虚 2024-12-26 09:45:14

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

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'
        ));
    }
}

另外,这里还有一些使用表和 Zend_Form 的资源

http ://www.packtpub.com/article/create-shopping-cart-using-zend-framework-2
http://davidcaylor.com/2008/03/ 24/building-table-based-forms-in-zend_form/
http://blog.kosev.net/2010/06 /tutorial-create-zend-framework-form.html

This might not be the prefect solution but it might help you!

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'
        ));
    }
}

Also, here are some resources to work with table and Zend_Form

http://www.packtpub.com/article/create-shopping-cart-using-zend-framework-2
http://davidcaylor.com/2008/03/24/building-table-based-forms-in-zend_form/
http://blog.kosev.net/2010/06/tutorial-create-zend-framework-form.html

千年*琉璃梦 2024-12-26 09:45:14

在我看来,你不能用视图助手来做你想做的事!

你应该做一个自定义的表单打印,例如:

<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>

lunch字段,不可能按照你的要求使用它,你应该考虑另一个策略。此刻我能想到!

you do not can to do what you want accomplish with a view helper, in my view!

you should make a custom form printing, such as:

<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>

lunch for the field, it is impossible to use it as you ask, you should think of another strategy. At the moment I can think of!

神经暖 2024-12-26 09:45:14

这是我的答案版本。

init中,我向表单添加了一个自定义装饰器RadioTable,并添加了加载装饰器的前缀路径

然后我将所有元素装饰为普通表格,这很重要,因为我所有的努力都是为了保留标准装饰器(例如“Error”)提供的设施

请注意单选元素中的Custom_Form_Element_FirstSelect->setSeparator

我还添加了提交按钮

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'
    ));
}

Custom_Form_Element_FirstSelect

class Custom_Form_Element_FirstSelect extends Zend_Form_Element_Select{

}

Custom_Form_Decorator_RadioTable

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;
    }
}

当然我可以使用元素的名称而不是使用自定义元素类来打开表格行,但这样我感觉更灵活。

Well here my version of the answer.

Into the init I've add a custom decorator RadioTable to the form, and prefix path to load the decorator

then I decorate all the elements as a normal table, this's important because all my efforts are to keep the facilities offer by standard decorators like "Error"

Please note the Custom_Form_Element_FirstSelect and the ->setSeparator in the radio element

I also add the submit button

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'
    ));
}

the Custom_Form_Element_FirstSelect

class Custom_Form_Element_FirstSelect extends Zend_Form_Element_Select{

}

the Custom_Form_Decorator_RadioTable

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;
    }
}

Of course I can use the name of the element instead of use a custom element class to open the table row, but in this way I'm feel more flexible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文