Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP: $form->input('checkbox');

Tags:

cakephp

with config div => false

$form->input('checkbox');

print

<input type="checkbox" value="1" ....>
<label>checkbox</label>

but i want it reverse order

<label>checkbox</label>
<input type="checkbox" value="1" ....>

can it reverse ?

like image 991
meotimdihia Avatar asked Aug 25 '10 03:08

meotimdihia


People also ask

How can I get form data in cakephp?

$data = $this->request->data; $someVariable = $data["name"]; Or, you can access any field directly, by using data() accessor: $someVariable = $this->request->data("name"); From this point, you can do anything you want with this variable.

What is FormHelper?

The FormHelper focuses on creating forms quickly, in a way that will streamline validation, re-population and layout. The FormHelper is also flexible - it will do almost everything for you using conventions, or you can use specific methods to get only what you need.


3 Answers

There's a neater way of doing this than the method ShiVik posted. That forced you to manually enter the title to checkbox inside of a tag. If you don't want that, there's a method to re-arrange the order of elements.

In your example, you simply want to change the order of the $format option, like so:

<?php echo $this->Form->input('checkbox', array(
                                  'type'=>'checkbox', 
                                  'format' => array('before', 'input', 'between', 'label', 'after', 'error' ) 
  ) ); ?>
  • Edit, just noticed your post was running cake 1.2. This code is for cake 1.3
like image 196
Paul Avatar answered Sep 29 '22 03:09

Paul


You can do this by setting the label to false and using the option "before" to display the label where you want.

<?php echo $form->input('checkbox', 
    array(
      'label'=>false, 
      'type'=>'checkbox',
      'before' => '<label>checkbox</lablel>', 
      'div' => false
 )); ?>

Useful links

  • http://book.cakephp.org/view/191/options-before-options-between-options-separator-a
  • http://book.cakephp.org/view/196/options-label

If not this, then you can use the Form element specific methods, instead of the automagic form elements.

like image 44
vikmalhotra Avatar answered Sep 29 '22 02:09

vikmalhotra


It's often easier to do things manually if the generic FormHelper::input wrapper doesn't fit your bill:

echo $form->label('fieldname');
echo $form->checkbox('fieldname');

I often don't use FormHelper::input beyond scaffolding.

like image 33
deceze Avatar answered Sep 29 '22 02:09

deceze