Once again thank you for your input Mon, I appreciate it!
It seems kind of odd that the developers of the form element view helpers
didn't take into consideration that the labels for radios and checkboxes might
want to be placed somewhere separate, no?
Furthermore, is it silly for me to say that I would have expected the elements
of multi form elements to be able to be addressed individually? For instance I
would have expected Form_Element_MultiCheckbox to be a collection of individual
Form_Element_Checkbox objects. This way, if I'm correct, the individual
elements placement would be more flexible, with something like:
$c = 0
foreach( $MultiCheckbox->getElements() as $checkbox )
{
echo $checkbox->renderLabel() . ' some separator ' .
$checkbox->renderViewHelper() . ( ++c % 3 == 0 ? ' another separator ' : '
default separator ';
}
For the current project this is a bit too elaborate to implement something
similar myself right now (as would your suggestion, but I'll keep it for future
reference). But perhaps I'll build something similar in the near future and
maybe even submit a proposal to ZF too then. That is, if it makes sense. ;-) Do
you think my reasoning makes sense? Or is there some logic which I'm currently
not seeing that prevents Form_Element_MultiCheckbox to be build from individual
Form_Element_Checkbox objects?
Cheers
Date: Sat, 7 Mar 2009 11:12:02 +0800
From: [email protected]
To: [email protected]
Subject: Re: [fw-general] Zend_Form: MultiCheckbox or Checkbox i.c.w.
ViewScript and looping through individual elements
The label wrapping logic is hardcoded in the view helper, specifically
Zend_View_Helper_FormRadio. Overriding that logic would involve a lot of code
duplication. If you're fine with that:
- create a file named MyMultiCheckbox.php in your application/views/helpers
directory
class Zend_View_Helper_MyMultiCheckbox extends
Zend_View_Helper_FormMultiCheckbox
{
public function myMultiCheckbox($name, $value = null, $attribs = null,
$options = null, $listsep = "<br />")
{
// stuff
}
}
- open the file Zend/View/Helper/FormRadio.php, copy the contents of the
formRadio() method to the method above and modify it to fit your needs. You
might also want to move the break-every-3-elements behavior here.
- tell the element to use your helper
$foo = new Zend_Form_Element_MultiCheckbox('foo', array(
'multiOptions' => $opts,
'decorators' => $decorators,
'helper' => 'MyMultiCheckbox'
));
-- Mon
On Sat, Mar 7, 2009 at 3:45 AM, <[email protected]> wrote:
Ah oke, I see what you mean now. No, I am using the ViewScript on the whole
form. So I have some more control over the placement of the elements in my
ViewScript. The PrepareElements decorator makes sure that the forms elements
are all made aware of the view and possible translators as far as I understand
it. Have a look at:
http://framework.zend.com/issues/browse/ZF-4812
Yes you are right about the decorator placement options of course. Now it even
makes more sense. In light of this, do you know of any way to make it so that
labels are always appended on form elements in stead of wrapped around. I tried
these thing:
// define decorators
$decorators = array(
'ViewHelper',
array( 'Label', array( 'placement' => 'append' ) ),
'Errors'
);
// another try
$decorators = array(
'ViewHelper',
array( 'Label', array( 'options' => array( 'placement' =>
'append' ) ) ),
'Errors'
);
// example of how I set the decorators on elements
$products = new Zend_Form_Element_MultiCheckbox( 'products' );
$products->setDecorators( $decorators )
->setLabel( 'products )
->setAttribs( array( 'class' => 'check' ) )
->setMultiOptions( $productList )
->setSeparator( ' ' );
But these decorator settings have no affect, it will still render the checkbox
elements like (notice label wrapped around element):
<label><input type="checkbox" [...]>The label</label>
In stead of:
<input type="checkbox" [...]><label>The label</label>
Cheers
Date: Sat, 7 Mar 2009 02:29:13 +0800
From: [email protected]
To: [email protected]
Subject: Re: [fw-general] Zend_Form: MultiCheckbox or Checkbox i.c.w.
ViewScript and looping through individual elements
No problem, I'm glad my suggestions worked for you.
By 'echoing the whole form', I mean you were doing a <?= $this->form ?> in the
view. I thought you were using ViewScript on the elements only and not the
form. renderForm() is only useful if you're doing something like
// controller
$this->view->form = new Form_Foo();
// view
<?= $this->form->renderForm(false) ?>
<?= $this->form->foo ?><br />
<?= $this->form->bar ?><br />
<?= $this->form->submit ?>
</form>
A decorator does not necessarily wrap the output of the preceding decorator(s)
though. It may append, prepend, wrap, or even completely ignore and replace the
previous output with something else. The default behavior of most decorators is
'append', so what happened there was the output of the ViewScript was appended
to the empty form which was returned by Form. (I don't know what
PrepareElements does, apparently it doesn't touch the string passed to it). The
'placement' option allows you to override the default behavior in some
decorators.
-- Mon
On Sat, Mar 7, 2009 at 1:24 AM, <[email protected]> wrote:
Works like a charm Mon. Thanks again.
I'm not sure though what you mean by echoing the whole form. If you mean echo
each individual element with $form->element->someElement->renderViewHelper()
then yes.
Anyway, the decorators are starting to make sense more and more now. The
decorator hierarchy, as I've read, does make a difference, as they wrap around
the former. I had some notion of the decorator pattern, but couldn't get my
head around it (no pun intended) too well at first. But this illustrates the
workings of a decorator pattern quite vivid. I guess the Form decorator got
overwritten by the ViewScript decorator when I had it the other way around.
Which makes perfect sense. It would be silly to have ViewScript wrap around the
Form decorator. If that is even possible at all.
Cheers
Date: Sat, 7 Mar 2009 00:55:59 +0800
From: [email protected]
To: [email protected]
Subject: Re: [fw-general] Zend_Form: MultiCheckbox or Checkbox i.c.w.
ViewScript and looping through individual elements
I see that you're echoing the whole form. There's no need for renderForm() in
that case. Just put the Form decorator after ViewScript. The output of the
ViewScript will be wrapped by <form> that way.
-- Mon
On Sat, Mar 7, 2009 at 12:33 AM, <[email protected]> wrote:
I gues I was a bit hasty about point 2. <?= $form->renderForm( false ); ?>
produces two form elements, one fully closed and one with only the starting tag:
<form enctype="application/x-www-form-urlencoded" accept-charset="utf-8"
class="offer" action="" method="post"></form>
<form enctype="application/x-www-form-urlencoded" accept-charset="utf-8"
class="offer" action="" method="post">
<dl>
<dt>
... etc
This is the code in my extended form:
$this->setName( $this->_name )
->setMethod( 'post' )
->setAttribs( array( 'accept-charset' =>
'utf-8', 'class' => 'offer' ) )
->setDecorators( array(
'Form',
'PrepareElements',
array( 'ViewScript', array(
'viewScript' => 'offer/partials/form.phtml' ) )
) );
Then from within my ViewScript:
<? $form = $this->element; ?>
<?= $form->renderForm( false ) . PHP_EOL; ?>
<dl>
<dt>
... etc
Any ideas of what might be going on here?
Cheers
From: [email protected]
To: [email protected]
Date: Fri, 6 Mar 2009 16:10:53 +0000
Subject: FW: [fw-general] Zend_Form: MultiCheckbox or Checkbox i.c.w.
ViewScript and looping through individual elements
Sorry guys, forgot to send to [email protected]. So here you go:
From: [email protected]
To: [email protected]
Subject: RE: [fw-general] Zend_Form: MultiCheckbox or Checkbox i.c.w.
ViewScript and looping through individual elements
Date: Fri, 6 Mar 2009 15:58:00 +0000
Date: Fri, 6 Mar 2009 18:26:25 +0800
From: [email protected]
To: [email protected]
Subject: Re: [fw-general] Zend_Form: MultiCheckbox or Checkbox i.c.w.
ViewScript and looping through individual elements
1. My suggestion is to take the string output of
$multiCheckbox->renderViewHelper(), explode it using the separator, then loop
through each line. e.g.
$form->addElement('multiCheckbox', 'foo', array(
'multiOptions' => $options,
'separator' => '__SEPARATOR__'
));// view script
$output = $this->element->renderViewHelper();$lines = explode('__SEPARATOR__',
$output);
$c = 0;
foreach ($lines as $line) { echo $line . (++$c % 3 == 0 ? '<br />' : '');
}Just make sure that you use a unique separator that won't cause the string to
split incorrectly.
2. If it's for rendering the form tag, <?php echo $form->renderForm(false) ?>
would output the opening <form> tag complete with the form attributes.
-- Mon
Hi Mon,
1) Good suggestion, seems like this is the way to go, yes. Thanks.
2) Yes! I was looking for something like that. But couldn't find it in de
apidoc nor in the Zend_Form class itself. Now I realize it is a decorator being
called through the magic __call() method. So I tried it just now, but couldn't
get it to work at first, but now I realize this was due to the fact that I had
overwritten the standard decorators with:
$this->setDecorators( array(
'PrepareElements',
array( 'ViewScript', array(
'viewScript' => 'offer/partials/form.phtml' )
);
Now that I added 'Form' as the first
element in the argument array all works fine.
Once again thank you for your valuable input. Zend Frameworks community is ace!
:-)
Cheers
On Fri, Mar 6, 2009 at 5:19 AM, <[email protected]> wrote:
Hi y'all,
I'm using a ViewScript decorator for my Zend_Form. In this form I'ld like each
third checkbox in a collection of checkboxes to start on a newline, or in a new
block element or something similar. I tried it with a MultiCheckbox.
I wish I could do something similar as beneath in my forms ViewScript:
<?
$c = 1;
foreach( $this->element->myMultiCheckbox as $checkbox )
{
$separator = $c % 2 == 0 ? '<br>' : '';
echo $checkbox->renderViewHelper() . $separator;
$c++;
}
?>
But sadly this doesn't seem possible. My next option I think would be to use
seperate Checkbox elements with the multiple [] indicator in their names. But I
have no idea how I would be able to loop through them in my ViewScript either.
Do you perhaps have any hints on how I could achieve this?
Also, with the Zend_Form ViewScript decorator, I thought I would be able to
access the form object with something like $this->form from within the view.
But I'm not able to access it. How would I be able to access for instance
Zend_Form::getMethod(), Zend_Form::getEncType() and other (userland) attributes
from within the ViewScript?
Thank you for your pointers.
Plan je feest, nodig mensen uit en deel je foto's met Windows Live Events
Deel en publiceer je favoriete foto's met Windows Live Photos
Ook nieuwsgierig naar de nieuwe Messenger? Download 'm hier
Twee keer zo leuk. Deel foto's terwijl je chat met de nieuwe Messenger
Twee keer zo leuk. Deel foto's terwijl je chat met de nieuwe Messenger
Twee keer zo leuk. Deel foto's terwijl je chat met de nieuwe Messenger
_________________________________________________________________
Jouw nieuws en entertainment, vind je op MSN.nl!
http://nl.msn.com/