How to create an input field without a label
If you use the form helper from CakePHP 1.2 to generate an input field, it automatically creates a label for the input field. So this snippet:
echo $form->input('Project/name');
will generate the following HTML code:
<div class="input">
<label for="ProjectName">Name</label>
<input name="data[Project][name]” type=”text” value=”" id=”ProjectName” />
</div>
But sometimes you don’t want a label. How can you accomplish that? My first two attempts failed, the label “Name” was still shown:
// first attempt
echo $form->input('Project/name', array('label' => ''));
// second attempt
echo $form->input('Project/name', array('label' => null));
With the third attempt I was then successful, and the label disappeared:
echo $form->input('Project/name', array('label' => false));
In retrospect the solution is logical, but sometimes you don’t see the obvious solution ;-)




I was wondering if you knew how to position a label for a checkbox? It always seems to appear to the right of a checkbox, can you make it appear to the left?
@theman: Yes, that’s possible, CSS is your friend. Something like
label { float: left; }or
input[type=checkbox] { float: right; }should do the trick.
Is it possible to set “label”=>false to be the default behaviour for all form elements?
Chris
I had to do something similar to disable the “div” it added.
Oh thank god.
I was having some problems with that and tried the first two you did but it never occured to me to try the last one.
@cdomigan: what i usually do with such things in 1.1.x.x is to create a helper which extends the HtmlHelper (or FormHelper in your case) then copy the function which you wish to be “different” from the original helper file to your newly created one. seems like a better way for me though it might not be the best way (i don’t know).
Hi Derick
I have issue with submit method in the html helper - the output was “input” but I prefer “button”.
I thought to make new helper witch extends HtmlHelper, but I wondered then if I need to use the newly created helper or the HtmlHelper.
I want to modify the submit method of the HtmlHelper, but without touching anything in the /cake dir. I prefer to stick to HtmlHelper, because it’s load automaticaly.
Thanks in advance
Miro
@miro: i guess it is sort of a preference thing.. like i’ve set labels on the checkboxes in my extended HtmlHelper etc. so I always use the extended version instead of the HtmlHelper.. this way you don’t need to edit the core files..
Dear god thank you so much, i had tried loads of other options
sometimes i think the name FormHelper is very misleading, it’s creating more work than the typing it saves!
@Sam: I am glad to hear this article helped you :)
Yeah, sometimes using the helper is more work than the traditional way and in some cases I even forgo to use the helper.
thx for this quick solution.
@gad: Good to hear this article was helpful for you :)
Thank you! I was looking all over for this. It was driving me nuts. I wish the documentation for 1.2 was a bit more instructive.
@Richard: I’m glad this article was helpful for you!
Thanks you! this was helpful but is there also a way to change the lable text?
for example I tried this with no success:
echo $form->input(’author’, array(’type’ => ‘text’, ‘lable’ => ‘Name of the Author:’));
@Neil: Yes, it is possible. Simply change “lable” to “label” in your example, and it should work ;-)
Thank you!!!
This solution may look simple but, it can get you stuck for quite some time…it took me a day until I found this blog!
Thank you once again :)
@Gayatri: I am glad this article helped you :)