سؤال

pretty much ripping my hair out over how badly designed the decorators are in Zend Framework. I've spent a lot of today and a few other days trying to figure out how to do something that should be a simple frontend task.

Zend_Forms and decorators in my opinion are the worst part of ZF as backend and frontend are not properly seperated. Why is the form class dictating how the HTML should be printed?

All I'm looking for, is something simple like:

<div class="labeledField">
    <label for="username">Username</label>
    <input type="text" id="username" name="username" />
</div>
<div class="labeledField">
    <label for="password">Password</label>
    <input type="password" id="password" name="password" />
</div>

The reason I want it like this is because I want the label to sit on top of the field like:

[Username       ]
[Password       ]

This way I can make the label slightly fade when the input is selected but still remain when the input is selected. Using JS the label is hidden when the input box contains anything. This functionality exists on the Apple shopping cart.

I love the validation parts of Zend_form and I would love how it can be placed in the frontend if backend code wasn't dictating how the HTML looks.

In my opinion it should either take a template (from a frontender with access to the /views/ folder or I should be able to do something like:

<div class="labeledField">
    <?php
    echo $this->form->username->getLabel();
    echo $this->form->username->getElement();
    $errors = $this->form->username->getErrors();
    if (sizeof($errors) > 0) {
        ?>
        <div class="errors">
        <?php
        foreach($errors as $error) {
            ?>
            <li><?php echo $error ?></li>
            <?php
        } 
        ?>
        </div>
        <?php
    }
    ?>
</div>

But then the templating would allow me to just echo $this->form and have it use the format as above for fields I want to.

هل كانت مفيدة؟

المحلول

No question about it, decorators can take some getting used to. Let me address/answer some of your issues/questions:

pretty much ripping my hair out over how badly designed the decorators are in Zend Framework. I've spent a lot of today and a few other days trying to figure out how to do something that should be a simple frontend task.

Yep, it's a miracle I have any hair left at all.

Zend_Forms and decorators in my opinion are the worst part of ZF as backend and frontend are not properly separated. Why is the form class dictating how the HTML should be printed?

I agree with you here that how the form is to be rendered is a presentation issue, so it's probably more view-related. If you really wanted to separate the concerns, you could create your forms undecorated and then add decorators in your view-script, perhaps using some custom view-helpers. This makes the most sense to me, though I confess I have never bothered to do it.

For examples of a standalone class that sets decorators on a separate form, check out EasyBib_Form_Decorator and Using Zend_Form without Zend Framework MVC

In my opinion it should either take a template (from a frontender with access to the /views/ folder or I should be able to do something like:

You pretty much do have that functionality using the ViewScript decorator.

$form->setDecorators(array(
    array('ViewScript', array(                      // note case
        'viewScript' => '_partials/forms/my.phtml', // note case
)));

That said, your desired markup is relatively straightforward using standard decorators:

// not tested, but should be pretty close
$element->setDecorators(array(
   'ViewHelper',
   'Label',
   'Errors',
   array('HtmlTag', array('tag' => 'div', 'attribs' => array('class' => 'labeledField'))),
));

Of course, any special styling or client-side manipulation of the form - for example, the Apple cart effect you cite - can be layered on to this markup on the client-side.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top