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

后端 未结 14 1474
你的背包
你的背包 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:51

    Unfortunately, the more complex issues are skipped over because without a real specific purpose or goal in mind, it's really hard to write an example or how-to. I spent some time helping out another developer here on SO who wanted to modify the way hidden elements were displayed, but his case was very specific so it was easier to drill down into specifics.

    The majority of the more complex tasks really come down to extending the default helper for a specific field type. For example, I had a project where I wanted to apply the class "error" to all fields that did not pass validation after a form had been submitted instead of writing out the validation error text:

    <label for="field">Label:</label>
    <input type="text" name="field" id="field" class="error">
    

    It was a fairly complex process because the actual form element is not available to the view helper by default, so the helper cannot check if the element has a validation error. So I went about the following process:

    1. I had to create a customer view helper for each field type that I wanted to apply the "error" class to. In it, I overrode the formXXX() method, and add a check to see if the element had an error. Additionally, I added a setElement() method that the decorator could call to set an instance of the element so my helper could check for errors.
    2. Next I had to override the default Zend_Form_Decorator_ViewHelper. In it, I accessed the view and instantiated the helper for the form element type and checked for the existence of the setElement() method I created in my view helper, setting it if it existed. By doing this, I could extend some form element types and not others without busting the entire script.
    3. In my init() function for each form, I had to add a new element prefix path (addElementPrefixPath('My_Form_Decorator'), 'path/to/decorator')) which pointed to my new form decorator.
    4. Finally, I had to add the helper path to my application so that Zend Framework could find the helpers I had created in the first bullet point: addHelperPath('/path/to/helpers', 'My_View_Helper');

    You can see why writing complex tutorials really requires real world problems. The great part about setting up these kinds of helpers, or complex decorator schemes is that, all be it annoying up front, it allows you to very easily create many forms that adhere to the same design scheme and modify their output uniformly very quickly if changes need to be made. But if on the other hand you feel like you're spending a great deal of time figuring out how to do what should be a simple task for the benefit of one form, I would say skip it!

    If you would like help for something more specific, however, I would be more than happy to help. Just pose another question (or update this one) and I'll do my best to help out.

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

    I'm not sure if Zend already has that feature but, a quick solution would be to extend the class , make it accept a html template.

    For example:

    <?php
    $form = new zend_form_whatever($field_list + $validators + $template);
    echo $form;
    ?>
    

    template:

    <form id="my_custom_form">
        <label>{label.email}</label>
        {input.email}
        <span class="red">{error.email}</span>
    
        <div>
            {label.password}
            {error.password}
            {input.password}
        </div>
    
        <div class="purple">{input.submit}<div>
    </form>
    
    0 讨论(0)
提交回复
热议问题