开发者

Html coders + zend form

开发者 https://www.devze.com 2023-02-17 01:38 出处:网络
So I use a zend framework, but for all commercial projects I am not using zend form component. HTML+Js coders are working on frontend and I don\'t want to have nothing with it, I just connect the fina

So I use a zend framework, but for all commercial projects I am not using zend form component. HTML+Js coders are working on frontend and I don't want to have nothing with it, I just connect the final view in my zf based application.

So for example I have in view a html form which looks like this:

http://pastie.org/1668143

开发者_如何学编程

So form design, elements and classes can be changed by frontend coders, and I want to know is there easy way for me, to use zend form with this concept (in each project form code looks different of course )?

My main problem here is code duplication in my controllers, which in this case looks something like this:

http://pastie.org/1668023 (please don't take exceptions and loggedMember seriously, they are just used for this snippet, to demonstrate a problem, which I have)

So, what would be the best solution for problem which I have, what do u think :) ?


If you have absolutely no control over the form's html structure, but still want to maximize the use of Zend_Form's features, use Zend_Form_Decorator_ViewScript.

http://framework.zend.com/manual/en/zend.form.standardDecorators.html (last section)

$element->setDecorators(array(array('ViewScript', array(
'viewScript' => '_element.phtml',
'class'      => 'form element'
))));

I would do it like this:

  1. create a form class that has all elements, validators and filters
  2. create an instance of the form in your action and set the view script(s) (this way you can change them per controller and still have very little duplicated definition code.


Splendid, I don't understand why you would have a problem with code duplication, in your 2nd link, you are performing your checks, then check if the page is a post request, then performing the checks again, yes its duplicated, but I don't understand what you are trying to explain by doing this?

As for the form, its up to you how you use it, you could create the form object, then instead of ever out putting the form, simply pass it the data from your designers form, and use it to validate things.
Or you could use custom templates for the form, OK it means you don't give the designers quite as much freedom of them designing a form and you sorting the results, but they can still do their best at it.
This is the setup I use, after all I am in charge of the functionality as the programmer, the designers just make it look good what the user see's.

So for example, if I want to create an input element I can:

$arrival_time = $this->createElement('text', 'arrival_time', array(
    'required' => true,
    'label' => 'Arrival Time:', 
));
$arrival_time->removeDecorator('HtmlTag');
$this->addElement($arrival_time);

Notice I have removed the HtmlTag decorators here - I don't need them for the markup as the designers will be arranging things for me.

Next thing to do is tell the form to use the layout the designers have made:

$this->setDecorators(array(array('ViewScript', array('viewScript' => 'NewArrivalsForm.phtml'))));

Here my template is within the view's, script's directory.

Now the designers have a few options. They could do:

<table border="0" width="100%">
    <tr>
        <td>
            <?php echo $this->element->arrival_time; ?>
        </td>

This will give you the following output:

<td>
    <dt id="arrival_time"><label for="arrival_time" class="required">Arrival Time:</label></dt>
    <input type="text" name="arrival_time" id="arrival_time" value="" />                                        
</td>

If there we're an error, that would be presented as well. You could remove the decorators 'Label', 'Description' & 'Errors' as well, to make it simply an input box:

<td>
    <input type="text" name="arrival_time" id="arrival_time" value="" />
</td>

Even once you have removed the decorators, the designers could still use for example:

<tr>
    <td>
        <?php echo $this->element->time_on_site->getLabel(); ?>
    </td>
</tr>
<tr>
    <td>
        <?php echo $this->element->time_on_site ?>
</td>

This will allow them to lay the form out exactly as they want to. But it will still allow you to use the full power of Zend_Form for your final validation checks. Take a look inside the Zend/form/element.php for all the methods you and your designers can use on the form element.

0

精彩评论

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