开发者

Zend_Form Prepend Element

开发者 https://www.devze.com 2022-12-21 14:04 出处:网络
I can add an element to a form like this: $form->addElement($element); However, that will put the element at the end of the form, I would like to prepend an element (put it at the beginning of t

I can add an element to a form like this:

$form->addElement($element);

However, that will put the element at the end of the form, I would like to prepend an element (put it at the beginning of the form).

Why? The form has dynamically generated fields (number of text fields and their labels are generated based on parameters from request) so the form class looks like this:

class Form1 extends Zend_Form
{

    public function init()
    {
        $this->setMethod('post');

        $submit = new Zend_Form_Element_Submit('submit1', array(
            'label' => 'Submit',
            'class' => 'input-submit'
        ));

        $this->addElements(array(
            $submit
        ));
    }
}

There 开发者_JAVA技巧is only the submit button because I don't know how many text fields and with what labels there will be yet.


From the ZF manual on Zend_Form Metadata and Attributes

Zend_Form_Element handles a variety of attributes and element metadata. Basic attributes include:

  • order: the index at which an element should appear in the form. Uses the setOrder() and getOrder() accessors.

So you would set the button to a very high order number, e.g. 1000 and then add the dynamic elements starting with an order number of 1 (or any number lower than the button's order number).

However, when there is nothing but the button in the form at all, then why not just create the entire form on the fly and append the submit button once you are done attaching the elements from the request.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号