Zend Form: How do I make it bend to my will?

后端 未结 14 1473
你的背包
你的背包 2021-01-29 18:53

I\'ve read the manual many times, I\'ve scoured the posts offered by Google on the subject, I have even bought a couple of books that deal with ZF. Now, why am I still confused?

相关标签:
14条回答
  • 2021-01-29 19:26

    ANSWER HERE. HOPE it will help you. I went through the enormal amount of information till I found it.

    <table>
        <tr>
            <td>Page name: *</td>
            <td><?php echo $this->pageForm->header_title;?></td>
        </tr>
    
        <tr>
            <td>H1 tag: *</td>
            <td><?php echo $this->pageForm->h1_tag;?></td>
        </tr>
    
        <tr>
            <td>Page URL: *</td>
            <td><?php echo $this->pageForm->url;?></td>
        </tr>
    
        <tr>
            <td>Meta Description: </td>
            <td><?php echo $this->pageForm->meta_description;?></td>
        </tr>
    
        <tr>
            <td>Meta Keywords: </td>
            <td><?php echo $this->pageForm->meta_keywords;?></td>
        </tr>
    
        <tr>
            <td>Short Description: *</td>
            <td><?php echo $this->pageForm->short_description;?></td>
        </tr>
    
        <tr>
            <td colspan="2"><a href="<?php echo $this->websiteUrl;?>backend_template/list?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=200&width=300&modal=true"
                               title="add a caption to title attribute / or leave blank" class="thickbox">Open iFrame Modal</a></td>
             <td>
                <a href="<?php echo $this->websiteUrl;?>backend_template/list?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=200&width=300&modal=true" onclick="javascript:getDataFromAddPage()" title="Select Template For Page" class="thickbox" > TEST FORM  </a>
             </td>
        </tr>
    
        <tr>
            <td>Featured: </td>
            <td><?php echo $this->pageForm->featured;?></td>
        </tr>
    
        <tr>
            <td>Mark as 404: </td>
            <td><?php echo $this->pageForm->is_404page;?></td>
        </tr>
    
        <tr>
            <td>Show in Navigation Menu: </td>
            <td><?php echo $this->pageForm->in_navmain;?></td>
        </tr>
    
        <tr>
            <td>Show in Static Menu:</td>
            <td><?php echo $this->pageForm->in_navstatic;?></td>
        </tr>
    
        <tr>
            <td><?php echo $this->pageForm->submit;?></td>
            <td><?php echo $this->pageForm->button_close;?></td>
        </tr>
    
    </table>
    
    0 讨论(0)
  • 2021-01-29 19:32

    To add to what was said:

    Dont be afraid of the decorators, you'll be using them lots if you decide to stick with Zend_Form. It's fairly straightforward once you 'get it', unfortunately the docs are weak and playing with it is almost the only way to get there.

    The ViewScript and ViewHelper decorators give you lots of power.

    Don't be afraid to dig through the source for the different decorators that exist and see how they're doing things (I gained a fair amount of insight comparing the original decorators with the dojo ones).

    I think what Sean was suggesting is that you don't need to call form->render, you can use something like this in your viewscript.

     <someHtmlThingy name="blah" value="<?php echo $this->formInstance->getElement('whatever')->getValue(); ?>" />
    

    I worked on one project (pre Zend_Form) where we built sets of validators and used standard viewscripts all the way through. It was a little more work (plumbing for error messages and the like), but not excessive compared to creating elements with Zend_Form.

    0 讨论(0)
  • 2021-01-29 19:32

    Skipping out on the form-wide decorators (IE - printing individual elements of the form rather than just relying on the Form's standard View Helper to handle everything) is one way to get a bunch of control back.

    Another option is to simply hand-code your form and only use ZF to handle validation and filtering.

    0 讨论(0)
  • 2021-01-29 19:33

    Currently we've got the new and shiny Zend\Form which is even more complex than the old component but also much more controllable, encapsulated and powerfull. So, what I said below doesn't apply. The new component is IMHO a huge improvement because it...

    • ...gives you full control over how you want to render your form, you need to write a bit more view code but it's worth it
    • ...separates data, logics and view to the maximally possible extent
    • ...makes use of the HTML5 form elements
    • ...gives you many options how you want to put your forms together, e.g. hydration, annotations, etc.

    Old answer regarding Zend_Form

    I can not really help probably because I have exactly the same problem. I think the Zend_Form decorators are powerful but by far the least programmer friendly and non-intuitive piece of ZF I've seen so far and I've used a major part of the ZF in various projects.

    That said I can only give a few hints from my experience:

    • filters are easy to use without using form
    • most if not all of the things you want to achieve are done with the help of decorators
    • I started with small forms and added stuff piece by piece but I don't get some of the stuff I'm doing in some of my forms because the result is based on trial and error (mainly with the decorators)
    • I've got information from the zend mailinglist which I couldn't have found anywhere on the web

    From the related questions to this on the right hand side it is obvious that there is a lack of comprehensive documentation for Zend_Form especially given it's non-intuitive nature. I would really like to see the ZF guys do something about this as much as I like Zend Framework.

    So this answer is probably just to let you know that you're not the only one having the this problem.


    or alternatively you could use jedi mind tricks of course!

    0 讨论(0)
  • 2021-01-29 19:34

    Sean McSomething's first advice is the way to go for me. I've always thought that doing <?php echo $this->form ?> to render a form is too magical. I'll take it a step further though and render individual decorators rather than individual elements via $element->renderDecorator() magic methods. This way, you won't have to worry about the order in which you defined your decorators. You just need them to be there, and you won't have to add those pesky, unintuitive HtmlTag decorators. I made a blog post about this.

    0 讨论(0)
  • 2021-01-29 19:37

    Barrett Conrad's advice is what I would have suggested. Also, keep in mind that you don't need to use a form object to render your form.

    One thing you could do is create a form in your view script that has elements with the same name as a form class.

    Your HTML form:

    <form action="/login/" method="post">
    <fieldset>
        <label for="username">Username:</label>
        <input type="text" size="10" name="username" />
        <label for="password">Password:</label>
        <input type="password" size="10" name="password" />
        <input type="submit" />
    </fieldset>
    </form>
    

    Your class:

    class LoginForm extends Zend_Form
    {
        public function init()
        {
            $username = $this->createElement('text','username');
            $username->setRequired(true);
            $this->addElement($username);
    
            $password = $this->createElement('password','password');
            $password->setRequired(true);
            $this->addElement($password);        
        }
    }
    

    Your form class reflects your HTML form, each element in your class has its own validators and requirements. Back in your action you can create an instance of your form class and validate your post/get vars against that form:

    $form = new LoginForm();
    if ($this->_request->isPost()) {
        if ($form->isValid($this->_request->getParams())) {
            // do whatever you need to do
        } else {
            $this->view->errors = $form->getMessages();
        }
    }
    

    You can display the the error messages at the top of your form in one group, using this method.

    This is a basic example, but it allows you to have total control over the presentation of your form without spending the time to learn to use decorators. Much of the strength of Zend_Form is in its validation and filtering properties, in my opinion. This gives you that strength. The main draw back to a solution like this is that your view script HTML form can become out-of-sync with your form class.

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