如何设置 Zend 表单元素的顺序并避免重复 [英] How can I set the order of Zend Form Elements and avoid duplicates

查看:26
本文介绍了如何设置 Zend 表单元素的顺序并避免重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Zend Form 中,如果两个元素具有相同的顺序,那么 Zend 将完全忽略第二个元素(而不是将其显示在第一个元素下).以下面的代码为例.请注意,CityZip Code 元素具有相同的 4

In Zend Form, if two elements have the same order, then Zend will totally ignores the second element (instead of displaying it under the first). Take the following code as an example. Notice that the City and Zip Code elements have the same order of 4

    $address = new Zend_Form_Element_Textarea('address');
    $address->setLabel('Address')
            ->setAttrib('cols', 20)
            ->setAttrib('rows', 2)
            ->setOrder(3)
            ;

    $city = new Zend_Form_Element_Text('city');
    $city->setLabel('City')
         ->setOrder(4)
          ;

    $postal = new Zend_Form_Element_Text('postal');
    $postal->setLabel('Zip Code')
           ->setOrder(4);

呈现此表单时,找不到 Zip Code 元素.

When this form renders, the Zip Code element is nowhere to be found.

如果我想动态地设置像按钮这样的元素,但告诉它在表单的末尾呈现,我该怎么做而不遇到两个元素具有相同顺序的问题?

If I want to set elements like a buttons dynamically, but tell it to render at the end of the form, how would I do this and not run into the problem of having two elements with the same order?

public function addSubmitButton($label = "Submit", $order = null)
{
    $form_name = $this->getName();

    // Convert Label to a lowercase no spaces handle
    $handle = strtolower(str_replace(" ","_",$label));

    $submit = new Zend_Form_Element_Submit($handle);
    $submit->setLabel($label)
           ->setAttrib('id', $form_name . "_" . $handle)
            ;

    /////////    Set the button order to be at the end of the form   /////////
    $submit->setOrder(??????);


    $this->addElement($submit);

}

推荐答案

如果你真的需要使用 setOrder() 方法,我会使用订单号 10, 20, 30, 40, ... 这样它将很容易在已经设置的元素之间添加元素.

If you really need to use the setOrder() method, I'd work with order numbers 10, 20, 30, 40, ... This way it will be easy to add elements in between already set Elements.

此外,为了避免使用两次订单号,您可以使用一个数组,在其中存储从 1 到 X 的所有数字.每当您设置订单号时,您都可以通过名为 getOrderNumberFromArray() 的方法来设置它返回数组中仍然可用的下一个更高或更低的顺序数并取消设置此数组元素.

Furthermore, in order to avoid using order-numbers twice, you could use an array, where you store all the numbers from 1 to X. Whenever you set an order number, you set it via a method called getOrderNumberFromArray() which returns the next higher or lower order number still available in the array and unsets this array element.

或者,也许更好,您可以在新元素之前对想要拥有的元素执行 getOrder(),然后将此订单号增加 X,然后遍历现有表单元素并检查订单号是否'尚不存在.

Alternatively, and maybe even better, you could do getOrder() on the element you want to have before the new element, then increment this order number by X and then loop through the existing form elements and check that the order number doesn't exist yet.

或者您可以在要在新元素之前和之后显示的元素上使用 getOrder() 并确保您不会为新元素使用相同的订单号.

Or you could just use getOrder() on the Element you want to show before and after the new element and make sure you don't use the same order numbers for the new element.

这篇关于如何设置 Zend 表单元素的顺序并避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆