How to properly customize CakePHP3 FormHelpers Radio buttons [duplicate] - cakephp

Cakephp 3 create a radio container with label -> input like that
<div class="radio">
<label class="radio-acces-checked" for="condition-access-1">
<input id="condition-access-1" type="radio" value="1" name="condition_access">
Free access
</label>
</div>
...
I would like change structure but it does not work, it's always the same strucure... Do you have an idea about how to solve my problem ?
$myTemplates = [
'radioWrapper' => '<div class="radio">{{label}}{{input}}</div>'
];
echo $this->Form->radio('condition_access', [
['value' => 1, 'text' => __('Free Access')],
['value' => 2, 'text' => __('Payment Access')],
['value' => 3, 'text' => __('Reduce price')]
]);

You need to set the nestingLabel template:
echo $this->Form->input('condition_access', [
'type' => 'radio',
'options' => [
['value' => 1, 'text' => __('Free Access')],
['value' => 2, 'text' => __('Payment Access')],
['value' => 3, 'text' => __('Reduce price')]
],
'templates' => [
'nestingLabel' => '{{hidden}}<label{{attrs}}>{{text}}</label>{{input}}',
'radioWrapper' => '<div class="radio">{{label}}</div>'
]
]);
Output:
<div class="input radio">
<label>Condition Access</label>
<input name="condition_access" value="" type="hidden">
<div class="radio">
<label for="condition-access-1">Free Access</label>
<input name="condition_access" value="1" id="condition-access-1" type="radio">
</div>
<div class="radio">
<label for="condition-access-2">Payment Access</label>
<input name="condition_access" value="2" id="condition-access-2" type="radio">
</div>
<div class="radio">
<label for="condition-access-3">Reduce price</label>
<input name="condition_access" value="3" id="condition-access-3" type="radio">
</div>
</div>

Related

can I add a 'span' after label using a FormHelper in CakePHP 3?

My code is:
echo $this->Form->control('select', [
'label' => 'sample label text',
'type' => 'select',
'multiple' => true,
'value' => [1,2],
'options' => ['a','b']
]);
The output is now:
<label class="control-label" for="select">sample label text</label>
<input type="hidden" name="select" value="">
<select name="select[]" multiple="multiple" id="select" class="form-control">
<option value="0">a</option>
<option value="1">b</option>
</select>
How should I change my code to add a span between the label and select?

cahephp formhelper template for generate chekbox

I am using cakephp formhelper and I want to generate checkbox
I want cakephp to generate checkbox like this
<div class="form-group form-md-line-input">
<label class="col-md-2 control-label"
for="form_control_1">checkbox_title</label>
<div class="col-md-10">
<div class="md-checkbox-inline">
<div class="md-checkbox">
<input id="***this_part_should_be_match"
class="md-check"
type="checkbox">
<label for="***this_part_should_be_match">
<span></span>
<span class="check"></span>
<span class="box"></span>
</label>
</div>
</div>
</div>
</div>
This formhelper config
[
'button' => '<button{{attrs}}>{{text}}</button>' ,
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>' ,
'checkboxFormGroup' => '<label class="col-lg-2 control-label">{{label}}</label><div class="col-md-10"> <div class="md-checkbox-list"><div class="md-checkbox"><input id="{{id}}" class="md-check" type="checkbox">
<label for="{{id}}">
<span></span>
<span class="check"></span>
<span class="box"></span></label></div></div></div>' ,
'checkboxWrapper' => '<div class="">{{label}}</div>' ,
'dateWidget' => '{{year}}{{month}}{{day}}{{hour}}{{minute}}{{second}}{{meridian}}' ,
'error' => '<div class="error-message">{{content}}</div>' ,
'errorList' => '<ul>{{content}}</ul>' ,
'errorItem' => '<li>{{text}}</li>' ,
'file' => '<input type="file" name="{{name}}"{{attrs}}>' ,
'fieldset' => '<fieldset{{attrs}}>{{content}}</fieldset>' ,
'formStart' => '<form class="form-horizontal" {{attrs}}><div class="form-body">' ,
'formEnd' => '</div> </form>' ,
'formGroup' => '{{label}}<div class="col-md-10">{{input}}<div class="form-control-focus"> </div></div>' ,
'hiddenBlock' => '<div style="display:none;">{{content}}</div>' ,
'input' => '<input class="form-control" type="{{type}}" name="{{name}}"{{attrs}}/>' ,
'inputSubmit' => '<input type="{{type}}"{{attrs}}/>' ,
'inputContainer' => '<div class="form-group form-md-line-input">{{content}}</div>' ,
// 'inputContainer' => '<div class="input {{type}}{{required}}">{{content}}</div>' ,
'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}{{error}}</div>' ,
'label' => '<label class="col-md-2 control-label" {{attrs}}>{{text}}</label>' ,
'nestingLabel' => '{{hidden}}<label{{attrs}}>{{input}}{{text}}</label>' ,
'legend' => '<legend>{{text}}</legend>' ,
'multicheckboxTitle' => '<legend>{{text}}</legend>' ,
'multicheckboxWrapper' => '<fieldset{{attrs}}>{{content}}</fieldset>' ,
'option' => '<option value="{{value}}"{{attrs}}>{{text}}</option>' ,
'optgroup' => '<optgroup label="{{label}}"{{attrs}}>{{content}}</optgroup>' ,
'select' => '<select class="form-control" name="{{name}}"{{attrs}}>{{content}}</select>' ,
'selectMultiple' => '<select name="{{name}}[]" multiple="multiple"{{attrs}}>{{content}}</select>' ,
'radio' => '<input type="radio" name="{{name}}" value="{{value}}"{{attrs}}>' ,
'radioWrapper' => '{{label}}' ,
'textarea' => '<textarea class="form-control" name="{{name}}"{{attrs}}>{{value}}</textarea>' ,
'submitContainer' => '<div class="">{{content}}</div>' ,
]
And currently it generate this
<div class="form-group form-md-line-input">
<label class="col-lg-2 control-label">
<input class="form-control"
name="active"
value="0"
type="hidden">
<label for="active">
<input name="active"
value="1"
id="active"
checked="checked"
type="checkbox">
Active
</label>
</label>
<div class="col-md-10">
<div class="md-checkbox-list">
<div class="md-checkbox">
<input id=""
class="md-check"
type="checkbox">
<label for="">
<span></span>
<span class="check"></span>
<span class="box"></span></label>
</div>
</div>
</div>
</div>
I'm wondering what should be my config to cakephp generate chekboxes as I want
in the theme I am using id="***this_part_should_be_match" and for="***this_part_should_be_match" should be the same to displays properly
I'm using cakephp 3.5 but I think it should be same in 3.*
I`ll appreciate any help
Add this to the config
'checkboxContainer' => '<div class="form-group form-md-line-input"><label class="col-md-2 control-label" for="form_control_1">{{title}}</label>
<div class="col-md-10">
<div class="md-checkbox-inline">
<div class="md-checkbox">
{{content}}{{label}}
<span></span>
<span class="check"></span>
<span class="box"></span>
</label>
</div>
</div>
</div>
</div>'
You'll have to set the title with the templateVars option when generating the checkbox
$this->Form->control('some_checkbox', ['type' => 'checkbox', 'templateVars' => ['title' => 'checkbox_title']);

Getting the right Checkbox Value in Array

I have a form where i collect data from checkboxes.
Here is my form
<div class="col-md-6">
<textarea name='description[]' placeholder="{{ Lang::get('core.description') }}" rows='6' id='description' class='form-control' required>
{{ $day->description }}
</textarea>
<div class="col-md-12">
<?php $meal = explode(",", $day->meal); ?>
<label class='checked checkbox-inline'>
<input type='checkbox' name='meal[]' value ='B' class='' #if(in_array('B',$meal))checked #endif />
{{ Lang::get('core.breakfast') }}
</label>
<label class='checked checkbox-inline'>
<input type='checkbox' name='meal[]' value ='L' class='' #if(in_array('L',$meal))checked #endif />
{{ Lang::get('core.lunch') }}
</label>
<label class='checked checkbox-inline'>
<input type='checkbox' name='meal[]' value ='D' class='' #if(in_array('D',$meal))checked #endif />
{{ Lang::get('core.dinner') }}
</label>
</div>
</div>
And this is my controller
$day = $_POST['day'] ;
for($i=0; $i < count($day); $i++)
{
$dataDays = array(
'day' => $_POST['day'][$i],
'title' => $_POST['title'][$i],
'meal' => implode(',', (array)$_POST['meal'] ),
'description' => $_POST['description'][$i],
'cityID' => $_POST['cityID'][$i],
'tourID' => $id
);
\DB::table('tour_detail')->insert($dataDays);
}
I get all the data right except meal. What am i missing with implode? I tried one without array as implode(',', $_POST['meal'] ), this didnt help. I tried implode(',', $_POST['meal'][$i] ) but i got an error. Can you please tell me how can i get the right data from checkboxes.
thanks

CakePHP & Twitter Bootstrap horizontal form with nested inline form

I am using the TwitterBootstrap Plugin for CakePHP2.x. And I can't get the combination of a horizontal form with a nested inline form to work.
part of my code:
<?php
echo $this->Form->create('Event', array('class' => 'form-horizontal'));
echo $this->Form->input('Event.short_name',array(
'label' => 'Short name',
'type' => 'text',
'class' => 'span5'
));
?>
<div class="control-group">
<div class="control-label">Date & Time</div>
<div class="controls form-inline">
<div class=" input-append">
<?php
echo $this->Form->input('Event.start_date', array(
'label' => false,
'type' => 'text',
'class' => 'span2 start_date',
'after' => '<span class="add-on datetime"><span class="icon-calendar"></span></span>'
));
echo $this->Form->input('Event.end_date', array(
'label' => false,
'type' => 'text',
'class' => 'span2 end_date',
'after' => '<span class="add-on"><span class="icon-calendar"></span></span>'
));
?>
</div>
</div>
</div>
In the above example this happens:
and the generated html
<div class="control-group">
<div class="control-label">Date & Time</div>
<div class="controls form-inline">
<div class=" input-append">
<div class="control-group">
<div class="controls">
<input name="data[Event][start_date]" class="span2 start_date" type="text" value="2013-06-15" id="EventStartDate" required="required"><span class="add-on"><span class="icon-calendar"></span></span>
</div>
</div>
<div class="control-group">
<div class="controls">
<input name="data[Event][end_date]" class="span2 end_date" type="text" value="2013-06-22" id="EventEndDate" required="required"><span class="add-on"><span class="icon-calendar"></span></span>
</div>
</div>
</div>
</div>
</div>
When I add 'div' -> false to the two date form field they move inline but the padding etc is gone.
and the generated html
<div class="control-group">
<div class="control-label">Date & Time</div>
<div class="controls form-inline">
<div class=" input-append">
<input name="data[Event][start_date]" class="span2 start_date" type="text" value="2013-06-15" id="EventStartDate" required="required"><span class="add-on"><span class="icon-calendar"></span></span>
<input name="data[Event][end_date]" class="span2 end_date" type="text" value="2013-06-22" id="EventEndDate" required="required"><span class="add-on"><span class="icon-calendar"></span></span>
</div>
</div>
</div>
I think this is due to the fact that all form fields are placed in controls divs... Does anyone know how to solve this?
I also posted this as an issue on the GitHub page, since I think it has to do with the plugin, but I hope someone knows a temp workarround.
Maybe this could work. I used it before and it looks like this:
<?php echo $this->Form->create('Event', array(
'class' => 'form-horizontal',
'inputDefaults' => array(
'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
'div' => array('class' => 'control-group'),
'label' => array('class' => 'control-label'),
'between' => '<div class="controls">',
'after' => '</div>',
'error' => array('attributes' => array(
'wrap' => 'span', 'class' => 'help-inline'
)),
)));?>
<fieldset>
<?php echo $this->Form->input('Event.start_date', array(
'label' => array('class' => 'control-label',
'text' => 'start date'),
));?>
</fieldset>
<?php echo $this->Form->end();?>
The issue might be conflict between TB and Cake about float and clear, because cake.generic.css sets for form div clear:both which moves elements below instead of next to.
Try this:
div.form-inline div{
clear: none;
}

Custom style on checkbox inside form div in cakephp

I have following line of code:
echo $form->input('terms', array('type' => 'checkbox', 'label' => 'I have read and accept the <a target="_blank" href="/terms">Terms</a>.'));
I want custom style on this checkbox. How can I do that in cakephp?
Problem is that error message that is appearing with check box is not aligned properly with other input text fields. So I want to align error message text, so I need either custom style or some other way to solve it. I am getting div of terms field in browser debugger, and when I changed there it works, but I don't know how to change div in cakephp? When I see .ctp file there is no div, so how will I change it. I am new in cakephp so please reply me in detail.
Just set 'label'=>false, and/or 'div'=>false then write your HTML, CSS...etc manually around it any any fashion you see fit.
More info here: CakePHP Book - Form Helper.
You can put a custom class on the field by adding 'class' => 'name' to the array and CakePHP will put that class on the input.
echo $form->input(
'terms',
array(
'type' => 'checkbox',
'class' => 'some-class-name',
'label' => 'I have read and accept the <a target="_blank" href="/terms">Terms</a>.'
));
Produces:
<div class="input checkbox">
<input type="hidden" name="" id="" value="0">
<input type="checkbox" name="" class="some-class-name" value="1" id="">
<label for="">I have read and accept the <a target="_blank" href="/terms">Terms</a>.</label>
</div>
You can apply custom styles to the input using 'style' => 'some:style;'
echo $form->input(
'terms',
array(
'type' => 'checkbox',
'style' => 'width:200px;',
'label' => 'I have read and accept the <a target="_blank" href="/terms">Terms</a>.'
));
Produces:
<div class="input checkbox">
<input type="hidden" name="" id="" value="0">
<input type="checkbox" name="" style="width:200px;" value="1" id="">
<label for="">I have read and accept the <a target="_blank" href="/terms">Terms</a>.</label>
</div>
You can also apply custom styles or classes on the <div> the input is grouped into and to the <label> associated with the <input> as well.
echo $form->input(
'terms',
array(
'type' => 'checkbox',
'label' => array(
'text' => 'I have read and accept the <a target="_blank" href="/terms">Terms</a>.',
'style' => 'width:200px;',
'class' => 'class-for-label'
),
'div' => array(
'style' => 'width:200px;',
'class' => 'class-for-div'
)
));
Produces:
<div class="class-for-div" style="width:200px;">
<input type="hidden" name="" id="" value="0">
<input type="checkbox" name="" value="1" id="">
<label for="" style="width:200px;" class="class-for-label">I have read and accept the <a target="_blank" href="/terms">Terms</a>.</label>
</div>
And finally, as #dave suggested, you can remove the <div> or <label> by setting them to false and insert your own custom HTML.
echo '<div class="input checkbox">';
echo $form->input(
'terms',
array(
'type' => 'checkbox',
'label' => false,
'div' => false
));
echo '<label>I have read and accept the <a target="_blank" href="/terms">Terms</a>.</label>';
echo '</div>';
Produces:
<div class="input checkbox">
<input type="hidden" name="" id="" value="0">
<input type="checkbox" name="" value="1" id="">
<label>I have read and accept the <a target="_blank" href="/terms">Terms</a>.</label>
</div>
source documentation
(I've deleted some element attributes because they are specific to the database, tables and models used)

Resources