How to print a group display individually from its content?

前端 未结 1 1055
广开言路
广开言路 2020-12-07 05:17

I\'m using Zend Framework and Zend_Form to render my form. But as I found it hard to customize it, I decided to print elements individually.

Problem is, I don\'t kno

1条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-07 05:58

    What you are looking for is the 'ViewScript' decorator. It allows you to form your html in any way you need. Here is a simple example of how it works:

    The form, a simple search form:

    createElement('text', 'query');
            // element options
            $query->setLabel('Search Keywords');
            $query->setAttribs(array('placeholder' => 'Query String',
                'size' => 27,
                ));
            // add the element to the form
            $this->addElement($query);
            //build submit button
            $submit = $this->createElement('submit', 'search');
            $submit->setLabel('Search Site');
            $this->addElement($submit);
        }
    }
    

    Next is the 'partial' this is the decorator, here is where you build the html how you want it:

    
    

    and finally the controller code:

    public function preDispatch() {
            //I put this in the preDispatch method because I use it for every action and have it assigned to a placeholder.
            //initiate form
            $searchForm = new Application_Form_Search();
            //set form action
            $searchForm->setAction('/index/display');
            //set label for submit button
            $searchForm->search->setLabel('Search Collection');
            //I add the decorator partial here. The partial .phtml lives under /views/scripts
            $searchForm->setDecorators(array(
                array('ViewScript', array(
                        'viewScript' => '_searchForm.phtml'
                ))
            ));
            //assign the search form to the layout place holder
            //substitute $this->view->form = $form; for a normal action/view
            $this->_helper->layout()->search = $searchForm;
        }
    

    display this form in your view script with the normal form ?>.

    You can use this method for any form you want to build with Zend_Form. So adding any element to your own fieldset would be simple.

    0 讨论(0)
提交回复
热议问题