zen of coding

A little something about the Form Helper…

Just a quick pointer about the form helper usage…

If you don’t like the default output of $form->input();

echo $form->input('SomeModel.some_field');

//which produces:
<div class="input text">
   <label for="SomeModelSomeField">Some Field</label>
   <input name="data[SomeModel][some_field]" type="text" value="" id="SomeModelSomeField" />
</div>

Mainly the div’s, it produces, you can of course turn them off by using:

echo $form->input('SomeModel.some_field', array('div'=>false));

//which produces:
<label for="SomeModelSomeField">Some Field</label>
<input name="data[SomeModel][some_field]" type="text" value="" id="SomeModelSomeField" />

But you can also specify an alternative wrapper tag:

echo $form->input('SomeModel.some_field', array('div'=>array('tag'=>'li')));

//which produces:
<li class="input text">
   <label for="SomeModelSomeField">Some Field</label>
   <input name="data[SomeModel][some_field]" type="text" value="" id="SomeModelSomeField" />
</li>

P.S I believe it was Mark Story, who’ve pointed this trick out to me. So thank him! ;)

  • I think this is illogical, because a div with a li tag is no longer a div ;-) A better solution would have been to use something like array(‘wrapperTag’ => ‘li’)

  • @Daniel Hofstetter

    Sure, but I assume this was much faster to implement.
    On the other hand we could read it like div -> replaced by -> tag -> [some tag] :)

    Either way, it’s more about the available option rather than implementation…

  • Dang…and here I set div to false and then manually broke in and out of PHP mode to add LIs. :P Thanks, mark!

  • @Brendon Kozlowski

    Yep, I actually used to do the same… :)

    Then I thought is there any good reason not to use div’s? … couldn’t find a reasonable answer as to why not, and decided to update my CSS instead.

  • I had an example CSS layout working with OL/LI, but was unable to convert it to work (cross-browser anyway) with simple DIV’s, so I had to do that (break in and out of PHP). Without going in to the details of it all, IE6 was being a pain and it took me less time to use what I had than to find an appropriate CSS-based solution to the issue. :)

    …otherwise, yeah, having extra DIV’s isn’t really all that big a deal when considering development time.

  • AxlF

    Hi,

    is there a possibility to set array(‘div’=>false) as default?

    It’s much work to write the array tag with every formfield…

    Regards,
    Alexander

  • @AxlF

    You can certainly modify the helper to allow that… but it would seem like extra work, since with CSS you can easily modify the div to behave like a span (for example), so it will not have any visual impact on your forms.
    And having a wrapper div, is quite nice for added flexibility.

%d bloggers like this: