Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Input HTML with CakePHP's FormHelper

Tags:

cakephp

I'm trying to use CakePHP's form helper to generate some input elements.

The HTML I am trying to generate is:

<div class="formRow">
    <label>LabelText:</label>
    <div class="formRight">
        <input name="data[User][email_address]" type="text" value="">
    </div>
    <div class="clear"></div>
</div>

Ive had a look through the Cake documentation (Using 2.1) and I can't find enough information on how to do this.

It looks like I need to use the format option on the input method, but can't figure out how to get it right. Especially concerned about the div surrounding the input field with a class name on it..

E.g. Ive tried something like this:

echo $this->Form->input('email_address', array(
                                                "input" => array('attributes' => array('wrap' => 'div','class' => 'formRight'))));

But this doesnt change any of the markup and just throws this error: Notice (8): Array to string conversion [CORE\Cake\View\Helper.php, line 459]

So my question is how can I get this form helper to create that markup?

Any help much appreciated

like image 688
cowls Avatar asked May 07 '12 00:05

cowls


1 Answers

You're over-thinking it. (No worries, we all do). Just remember, CakePHP is all about making things easier for you (among other things) - if you're struggling with trying to force Cake to do something for you, just remember, you can fall back to the basics - it's just PHP/HTML after-all.

<div class="formRow">
    <label>LabelText:</label>
    <div class="formRight">
        <?php echo $this->Form->input('email_address', array(
            'div'=>false, 'label'=>false)); ?>
    </div>
    <div class="clear"></div>
</div>

You should use the Form helper for your forms when possible, but you don't have to use all of it's presets like surrounding divs & labels. In the case above, just tell it you don't want the div, and wrap it with a div yourself.

If you don't want <div>s or <label>s around any inputs, you can also set the form's inputDefaults:

$this->Form->create('Whatever', array(
    'inputDefaults' => array('label'=>false, 'div'=>false)
));
like image 121
Dave Avatar answered Nov 08 '22 04:11

Dave