nuts and bolts of cakephp

A little something about the Form Helper…

Posted in CakePHP by teknoid on November 17, 2008

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! ;)

7 Responses

Subscribe to comments with RSS.

  1. Daniel Hofstetter said, on November 18, 2008 at 4:20 am

    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’)

  2. teknoid said, on November 18, 2008 at 8:48 am

    @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…

  3. Brendon Kozlowski said, on November 18, 2008 at 11:31 am

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

  4. teknoid said, on November 18, 2008 at 11:35 am

    @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.

  5. Brendon Kozlowski said, on November 18, 2008 at 5:53 pm

    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.

  6. AxlF said, on November 23, 2008 at 8:18 am

    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

  7. teknoid said, on November 24, 2008 at 12:41 pm

    @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.


Leave a Reply