5

Is there a reliable way to create a custom view for a Zend_Form? The decorators are pretty cryptic and using them in fancy ways sometimes is so complicated that I'd prefer to just write the HTML by hand. Is there a way to do it and still make the form fully cooperate with the controller (eg. call $form->isValid() and expect everything to validate properly)? If so, are there any caveats to look out for (like taking care about validation errors display)?

The ideal solution would be to create a form and pass the elements array (containing the necessary data like names, IDs, input types and all needed to render the HTML) - does Zend Framework permit this?

[EDIT]

Instead of just echoing the form object, I have tried adding this code in the view (a simple login form):

<?php
$userid = $this->form->getElement('userid');
$pass = $this->form->getElement('password');
$remember = $this->form->getElement('remember');
$submit = $this->form->getElement('submit');
?>
<form enctype="<?php echo $this->form->getEnctype(); ?>" method="<?php echo $this->form->getMethod(); ?>" action="<?php echo $this->form->getAction(); ?>" id="<?php echo $this->form->getId(); ?>">
name: <input type="text" id="<?php echo $userid->getId(); ?>" name="<?php echo $userid->getName(); ?>" /><br />
pass: <input type="password" id="<?php echo $pass->getId(); ?>" name="<?php echo $pass->getName(); ?>" /><br />
remember: <input type="checkbox" id="<?php echo $remember->getId(); ?>" name="<?php echo $remember->getName(); ?>" /><br />
submit: <input type="submit" id="<?php echo $submit->getId(); ?>" name="<?php echo $submit->getName(); ?>" value="<?php echo $submit->getValue(); ?>" />
</form>

The form seems to work OK and validate (although I don't get redirected the the page I came from - but that's a different problem, I believe, as I pass that via GET, not in the form). Is that acceptable, or am I doing something horridly wrong without knowing it?

mingos
  • 23,778
  • 12
  • 70
  • 107
  • You can populate zend_forms with data using $form->populate($data); – Marcin Jan 28 '11 at 12:09
  • I don't mean populating them with data, I mean writing my own HTML, without recurring to using those terrible, terrible decorators. – mingos Jan 28 '11 at 12:11
  • It is a messy method, and it's why I won't post it as an answer, but as Marcin suggested, you can still write your own form in a view, with the exact same ids, and write your form with Zend_Form, and then populate it to use filters, validators, etc... You can also use standalone filters & validators without using Zend_Form at all, basicly it is the same as using Zend_Form without the generated form. But the best advise is still: dive into decorators ;) – Boris Guéry Jan 28 '11 at 12:22
  • @mingos. So maybe you could create your own form in html, and than populate the zend_form with $_POST after submitting it. Off course you would need to have a zend_form with all the corresponding elements, validators and filters that you want to use. You would only populate it and use for validation of the data submitted, rather than for displaying in the view. There are also view helpers for manual creation of forms, maybe they could be helpful in such situation. – Marcin Jan 28 '11 at 12:27
  • I tested this out and the results are pretty decent. Mind having a look at the edit? – mingos Jan 28 '11 at 12:56
  • @mingos. I think that there is no universal answer whether it is good or bad. As long as it works, it is ok. However, I'm not sure whether you will save much time doing this task this way. Because now you need to e.g. add html for error messages display or to add hidden fields for check-boxes in case they are not checked. All this is done for you by zend_form. So maybe it is worth spending some time on understanding decorators, as in the long term, it will reduce the time needed to make and manage forms. – Marcin Jan 28 '11 at 13:26
  • @Marcin: I will not save time on EVERY form, obviously. But I need this for a particular form that has some really funky layout and the decorators go crazy with it. I assume I'll still let Zend do its normal form rendering 99% of the time :). – mingos Jan 28 '11 at 13:36
  • @mingos. Yes, decorators are a bit strange. Maybe in ZF 2 they will make them more 'user friendly'. Anyway, good lack with your form. – Marcin Jan 28 '11 at 13:57
  • @mingos This isn't wise thinking. Maybe you should ask how to create the form layout you need with decorators? They are really simple and straightforward to use. – takeshin Jan 28 '11 at 18:51
  • @takeshin: I'm sure learning all the magic behind decorators would be an awesome solution (I admit I'm not well versed in that field), but I'd love to know a way to avoid writing the HTML manually. Let me give you an example of what I don't know how to achieve. In the forum thread creation form, there's ~30 thread icons to choose from. The icons are displayed in, let's say, 5 rows & 6 columns table, each one with its corresponding radio button. All other fields use the default `DtDdWrapper`. Do you think this could be done using only decorators? If so, I'd love to learn about it. – mingos Jan 28 '11 at 19:31
  • Ah, one more thing: the icons form elements are autogenerated (from the contents of the icons directory) in a loop, so I never know beforehand how many items the form will have. – mingos Jan 28 '11 at 19:33
  • @mingos I have seen some time ago similar question on SO, with screenshot illustrating layout of the radios. This looks like a quite complicated form. Will it be usable and accessible? – takeshin Jan 28 '11 at 21:09
  • @takeshin: it is useable and accessible. It's been on the portal for ages and the users seem to be quite used to it, we just add/remove/replace the icons sporadically. But it's been implemented as a separate view with mostly hand-written HTML. I find it a bit sloppy. – mingos Jan 29 '11 at 03:40

1 Answers1

2

A lot depends of the design and the final layout.

Where do you want to display validation errors? How? Via Error decorator?
Do you want also use descriptions?
Do you need to use other decorators?

The best way seems to be to create just your own Zend_Form_Element_ThreadIcons element. This is as easy as subclassing one of Zend_Form elements and implementing custom _render() method returning HTML you need. You may even use your own View instance there.

Then you may pass array of icons as an element option and handle it the way you need.

If you decide using decorators, you my find this presentation very useful to master the technique:

takeshin
  • 49,108
  • 32
  • 120
  • 164