Form checkbox and label with CakePHP and Bootstrap - cakephp

I'm having some issues trying to get checkbox inputs and labels to output the correct HTML using CakePHP and Twitter Bootstrap.
The Bootstrap specific output should be:
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Keep me logged in
</label>
</div>
</div>
However, using the inputDefaults mentioned here (http://stackoverflow.com/a/9496242/1247225), this Cake form input:
echo $form->input('auto_login', array(
'type' => 'checkbox',
'label' => __('Keep me logged in', true)));
Outputs this:
<div class="control-group">
<input type="hidden" name="data[User][auto_login]" id="UserAutoLogin_" value="0" />
<input type="checkbox" name="data[User][auto_login]" class="" value="1" id="UserAutoLogin" />
<div class="controls">
<label for="UserAutoLogin">Keep me logged in</label>
</div>
</div>
Any ideas how to adjust this individual input so it outputs the correct Bootstrap HTML, like above?

The best way to control the form output, or the format of the output, is by passing a 'format' argument. Try playing with this:
'format' => array('before', 'label', 'input', 'between', 'after', 'error')
I don't think it is document, but by reading the code, you can find it in there.

the prettiest way to do it is this:
<?php
echo '<div class="col-lg-2">';
echo $this->Form->input('Content.checkbox', array(
'div' => array(
'class' => 'input-group',
),
'label' => false,
'type' => 'checkbox',
'before' => '<span class="input-group-addon">',
'after' => '</span><span class="input-group-addon"><label for="ContentCheckbox">What does the fox say?</label></span>',
));
echo '</div>';

I using :
<?php
echo $this->Form->input('coupDeCoeur',
array('div' => false,
'label' => false,
'type' => 'checkbox',
'before' => '<label class="checkbox">',
'after' => '<i></i>coupDeCoeur</label>'
));
?>

You need to build the form widget manually for checkboxes instead of using FormHelper::input.
For example:
echo '<div class="control-group">';
echo $this->Form->label('Model.field', null, array('class' => 'control-label'));
echo '<div class="controls">';
echo $this->Form->checkbox('Model.field');
echo '</div>';
echo '</div>';

Try following code:
<div class="control-group">
<div class="controls">
<label class="checkbox">
<?php echo $this->Form->input('auto_login', array('type'=>'checkbox','label' => false, 'div' => false,'class'=>false,'after'=>__('Keep me logged in')));?>
</label>
</div>
</div>

If you're using security component, you may have problems if you create your inputs without FormHelper::input. If that's the case, you should try with this:
echo "
<div class='control-group'>
<div class='controls'>
<label class='checkbox'>";
echo $this->Form->input('Model.field', array('label' => false, 'after' => 'Model.field'))."
</label>
</div>
</div>";

Here is the ready-to-use solution:
App::uses('FormHelper', 'View/Helper');
class InputHelper extends FormHelper {
// var $helpers = array('Form', 'Html');
public function create($model, $options = array()) {
$options['class'] = (isset($options['class']) && $options['class']) ? $options['class'] : 'form-horizontal table';
$options['inputDefaults'] = (isset($options['inputDefaults']) && $options['inputDefaults']) ? $options['inputDefaults'] : array(
'div' => 'control-group',
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>'
);
return parent::create($model, $options);
}
public function input($fieldName, $options = array()) {
$this->setEntity($fieldName);
$options = $this->_parseOptions($options);
if ($options['type'] == 'checkbox') {
$options['format'] = array('before', 'label', 'between', 'input', 'after', 'error');
}
fdebug($options);
return parent::input($fieldName, $options);
}
}

If you want a "one line" answer i got it:
echo $this->Form->input('Model.Field', array('class'=>'form-inline', 'after'=>'</br>'));

In my case, I used AdminLTE template with Bootstrap 3 and this code was working for me:
<?php
echo $this->Form->input('Model.fieldname', array(
'label' => false,
'type' => 'checkbox',
'before' => '<label>',
'after' => 'Field name label here</label>',
));
?>
Good luck here!!!

Here's a simple solution that works for me:
The CSS (LESS):
input.form-control[type="checkbox"] {
width: auto;
height: auto;
margin-right: 5px;
display: inline;
}
Then use the Form helper:
echo $this->Form->input(
'field_name',
array(
'div' => 'form-group',
'class' => 'form-control',
'type' => 'checkbox',
'label' => array(
'text' => 'Your label',
'class' => 'label-checkbox'
),
'format' => array('input', 'label')
)
);

Related

Why my variables are undefined i click on the submit button?

I have fields in a form with variables from the Controller TownsController, I use "set" to be able to affchés in sight. I arrive at the desired result, the problem is that when I want to send my information by clicking on the submit button, I have several errors telling me that the variable is undefined.
Before i click on the submit button :
After i click on the submit button :
The view :
<h2>Informations de votre ville</h2>
<?php echo $this->Form->create('Town'); ?>
<div class="form-group">
<div class="col-xs-6">
<?php echo $this->Form->input('statut',array(
'label' => 'Statut',
'type' => 'select',
'options' => array(
'Village'=>'Village',
'Ville'=>'Ville'),
'selected' => $selectedstatut,
'class' => 'form-control',
'id' => 'town_statut_municipal',
'empty' => __('Statut municipal')
));
?>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<?php echo $this->Form->input('name', array('type' => 'text', 'id' => 'town_name', 'class' => 'form-control', 'placeholder' => 'Choisir un statut municipal', 'label' => 'Nom')); ?>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<?php echo $this->Form->input('country', array('type' => 'select', 'class' => 'form-control', 'label' => 'Pays', 'id' => 'town_country', 'onchange' => "print_state('state',this.selectedIndex);", 'selected' => $selectedcountry, 'empty' => false,
'options' => array(
'Canada'=>'Canada',
'USA'=>'USA'))); ?>
</div>
</div>
<div class="form-group">
<div class="col-xs-6">
<?php
echo $this->Form->input('localisation', array('type' => 'select', 'class' => 'form-control', 'label' => 'Localisation', 'id' => 'state', 'empty' => $namelocalisation,
'options' => $localisations)); ?>
</div>
</div>
<div class="form-group">
<div class="col-xs-12">
<br>
<?php
$options = array('label' => 'Sauvegarder', 'class' => 'btn', 'div' => false, 'class' => 'btn btn-lg btn-success');
echo $this->Form->end($options);
?>
</div>
</div>
</form>
TownsController (edit view) :
public function edit(){
$town_id = $this->Auth->user('town_id');
$user_id = $this->Auth->user('user_id');
if(!$town_id && !$user_id) {
$this->redirect('/');
die();
}
$this->Town->id = $town_id;
if($this->request->is('put') || $this->request->is('post')) {
}
else {
$this->request->data = $this->Town->read(array('name', 'country', 'localisation', 'statut'), null);
$selectedstatut = $this->Session->read('Auth.Town.statut');
$this->set(compact('selectedstatut'));
$selectedcountry = $this->Session->read('Auth.Town.country');
$this->set(compact('selectedcountry'));
$countryuser = $this->Session->read('Auth.Town.country');
if($countryuser = 'Canada') {
$namelocalisation = 'Province';
$localisations = array(
'Alberta'=>"Alberta",
'British Columbia'=>"British Columbia",
'Manitoba'=>"Manitoba",
'New Brunswick'=>"New Brunswick",
'Newfoundland'=>"Newfoundland",
'Northwest Territories'=>"Northwest Territories",
'Nova Scotia'=>"Nova Scotia",
'Nunavut'=>"Nunavut",
'Ontario'=>"Ontario",
'Prince Edward Island'=>"Prince Edward Island",
'Quebec'=>"Quebec",
'Saskatchewan'=>"Saskatchewan",
'Yukon Territory'=>"Yukon Territory");
}
if($countryuser = 'USA') {
$namelocalisation = 'État';
$localisations = array(
'Alabama'=>"Alabama",
'Alaska'=>"Alaska",
'Arizona'=>"Arizona",
'Arkansas'=>"Arkansas",
'California'=>"California",
'Colorado'=>"Colorado",
'Connecticut'=>"Connecticut",
'Delaware'=>"Delaware",
'District Of Columbia'=>"District Of Columbia",
'Florida'=>"Florida",
'Georgia'=>"Georgia",
'Hawaii'=>"Hawaii",
'Idaho'=>"Idaho",
'Illinois'=>"Illinois",
'Indiana'=>"Indiana",
'Iowa'=>"Iowa",
'Kansas'=>"Kansas",
'Kentucky'=>"Kentucky",
'Louisiana'=>"Louisiana",
'Maine'=>"Maine",
'Maryland'=>"Maryland",
'Massachusetts'=>"Massachusetts",
'Michigan'=>"Michigan",
'Minnesota'=>"Minnesota",
'Mississippi'=>"Mississippi",
'Missouri'=>"Missouri",
'Montana'=>"Montana",
'Nebraska'=>"Nebraska",
'Nevada'=>"Nevada",
'New Hampshire'=>"New Hampshire",
'New Jersey'=>"New Jersey",
'New Mexico'=>"New Mexico",
'New York'=>"New York",
'North Carolina'=>"North Carolina",
'North Dakota'=>"North Dakota",
'Ohio'=>"Ohio",
'Oklahoma'=>"Oklahoma",
'Oregon'=>"Oregon",
'Pennsylvania'=>"Pennsylvania",
'Rhode Island'=>"Rhode Island",
'South Carolina'=>"South Carolina",
'South Dakota'=>"South Dakota",
'Tennessee'=>"Tennessee",
'Texas'=>"Texas",
'Utah'=>"Utah",
'Vermont'=>"Vermont",
'Virginia'=>"Virginia",
'Washington'=>"Washington",
'West Virginia'=>"West Virginia",
'Wisconsin'=>"Wisconsin",
'Wyoming'=>"Wyoming");
}
$this->set('localisations', $localisations);
$this->set('namelocalisation', $namelocalisation);
}
}
The reason is that you set those variables IF the request is not POST. When you click on Submit your code will enter in the "isPost" condition, hence the variables localisations, namelocalisation, selectedcountry and selectedstatut are never set.
Here's what I would do (pseudo code)
if ($this->request->is(array('post', 'put'))) {
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('Your post has been updated.'));
return $this->redirect(array('action' => 'index'));
}
}
$selectedstatut = $this->Session->read('Auth.Town.statut');
etc...
Notice I am setting the view variables outside the if statement so that if save fails (because validation) then we set the variables again.

How to add a div to a radio button?

I'm a new user of CakePHP.
I'm trying to add some div to contain my input + my label.
This is what I've got :
<?php
$option = array ("value1" => "labelContent1", "value2" => "abelContent2");
echo $this->Form->input('name', array('type' => 'radio', 'options' => $option, 'div' => true, "legend" => false));
?>
<div class="input radio">
<input type="hidden" name="data[Quiz][name]" id="ModelName_" value=""/>
<input type="radio" name="data[Quiz][name]" id="ModelName1" value="value1" />
<label for="ModelName1">labelContent1</label>
<input type="radio" name="data[Quiz][name]" id="ModelName2" value="value2" />
<label for="ModelName2">labelContent2</label>
</div>
And this what I would like to have :
<div class="input radio">
<input type="hidden" name="data[Quiz][name]" id="ModelName_" value=""/>
<div>
<input type="radio" name="data[Quiz][name]" id="ModelName1" value="value1" />
<label for="ModelName1">labelContent1</label>
</div>
<div>
<input type="radio" name="data[Quiz][name]" id="ModelName2" value="value2" />
<label for="ModelName2">labelContent2</label>
</div>
</div>
Do you know if it is possible to make it by using the FormHelper ?
I know it's an old thread but I had to reply.
The correct way to achieve this without creating a bad mark-up is to use before and after options:
echo $this->Form->input('name', array( 'type' => 'radio',
'separator'=> '</div><div>',
'before' => '<div>',
'after' => '</div>',
'options' => $option,
'label' => true,
"legend" => false
)
);
It looks like you can't achieve that with the Form Helper. The radio options are not wrapped in anything. See code.
You can add 'separator' => '<br/>' to the Form->input options, to display each option in its own line.
or implement it yourself.
you can try this
$option = array ("value1" => "labelContent1", "value2" => "abelContent2");
echo $this->Form->input('name', array( 'type' => 'radio',
'separator'=> '</div><div>',
'options' => $option,
'label' => true,
"legend" => false
)
);
<?php
$option = array ("value1" => "labelContent1", "value2" => "abelContent2");
echo $this->Form->input('name', array(
'type' => 'radio',
'options' => $option,
'templates' => ['radioWrapper' => '<div>{{label}}</div>'],
"legend" => false));
?>
This seems to work well for Cake 3.+

CakePHP 2.1 create select box

I am trying to add below style..
<div class="rowElem noborder">
<label>Language:</label>
<div class="formRight noSearch">
<select name="select2" class="chzn-select">
<option value="opt1">Choose the Language</option>
<option value="opt2" selected="selected">Kannada</option>
<option value="opt3">Telugu</option>
<option value="opt4">Tamil</option>
</select>
</div>
<div class="fix"></div>
</div>
But in cakephp, I have this code
<?php echo $this->Form->input('language_id', array('class' => 'chzn-select' )); ?>
Please give me the solution..
If I understand what you are asking, this is what you need to do.
In your controller you will create your options array for the select box:
$this->set('languageOptions', array('opt1' => 'Choose Language', 'opt2' => 'Kannada', 'opt3' => 'Telugu', 'opt4' => 'Tamil'));
Then in the view, you create the form:
<div class="rowElem noborder">
<label for="language_id">Language:</label>
<?php echo $this->Form->input('language_id', array('class' => 'chzn-select', 'options' => $languageOptions, 'label' => false, 'div' => array('class' => 'formRight noSearch'))); ?>
<div class="fix"></div>
</div>
$langs = array('opt1' => 'Choose Language', 'opt2' => 'Kannada', 'opt3' => 'Telugu', 'opt4' => 'Tamil');
$this->set(compact('langs')); // if you set options from controller
Then in view try this:
$this->Form->input('language_id', array(
'type' => 'select',
'options' => $langs,
'selected' => 2 // suppose default select Kannada
)
);
Cake Php Select Option Code
Language:
Form->input('language_id', array('class' => 'chzn-select', 'options' => $languageOptions, 'label' => false, 'div' => array('class' => 'formRight noSearch'))); ?>

cakephp label in checkbox

i'm trying to get a checkbox with his label
echo $this->Form->checkbox('straordinari', array('div'=>'true', 'label' => 'Straordinari'));
in browser i get
<input id="ReportStraordinari_" type="hidden" value="0" name="data[Report][straordinari]">
<input id="ReportStraordinari" type="checkbox" value="1" label="Straordinari" div="true" name="data[Report][straordinari]">
but there is no label
where is the problem?
You should get what you are looking for with the following:
echo $this->Form->input('straordinari', array('type' => 'checkbox'));
I using :
<?php
echo $this->Form->input('coupDeCoeur',
array('div' => false,
'label' => false,
'type' => 'checkbox',
'before' => '<label class="checkbox">',
'after' => '<i></i>coupDeCoeur</label>'
));
?>

Add label for submit button

I'm new in cakephp. What I try to accomplish is this output :
<p><label> </label><input class="adminbut rad2" type="submit" name="submit" value="Login" /></p>
And this is what I did in my view file
<?php echo $this->Form->end(array(
'div' => false,
'label' => 'Login',
'class' => 'adminbut rad2',
'name' => 'submit',
'value' => 'Login',
'before' => '<p>',
'after' => '</p>'
));?>
And what I got is :
<input class="adminbut rad2" name="submit" value="Login" type="submit" /></p>
And as you can see, my output is missing :
<label> </label>
Any solution?
Thanks :)
Try
$form->create();
$form->submit("Login",array( 'div' => false, 'class' => 'adminbut rad2', 'name' => 'submit', 'value' => 'Login', 'before' => '<p><label> </label>', 'after'
=> '</p>'
));
$form->end();
echo $form->input('submit', array(
'type'=>'submit',
'value'=>'Login',
'class'=>'adminbut rad2',
'div'=>array('tag'=>'p'),
'label'=>" "
));
Try This my friend
Cakephp 2.X
$this->Form->submit(__('Submit'), array('class'=>'adminbut rad2'));
Cakephp 1.x
$form->submit(__('Submit'), array('class'=>'adminbut rad2'));

Resources