9

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

3 Answers 3

15

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)
));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that does the trick and gives me more flexibility going forward :)
@Dave Comforting words. Thank you.
0

CakePHP 5 info, for those coming from search.

You can use your own templates. They will be stored under APP/config in a specific file. You will pass the name of that file to every form that has to use the templates. More info: https://book.cakephp.org/5/en/views/helpers/form.html#customizing-the-templates-formhelper-uses

Also here's the list of the built-in templates, maybe what you need is already there: https://github.com/cakephp/cakephp/blob/a49776e545b72adbf7650d48cd0f393b3a4263aa/src/View/Helper/FormHelper.php#L99

Comments

-1

If you have a lot of fields, you can use Jquery.

php:

echo $this->Form->input('email_address', array('class' => 'formRow'));

Jquery:

$(".formRow").each(function() {
    $(this).wrapInner( "<div class='formRight'></div>");
    $(this).find("label").prependTo(this);
    $(this).append('<div class="clear"></div>');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.