Cakephp Form Radio Button Label Class - cakephp

I want to use Radio Button using the Form Helper. A radio button has a Radio element and a Label. I have by default Display: block for Label element. I want to assign a class the the label of the Radio button so that i can assign it a inline-block.
$attributes = array('legend' => false, 'label' => array('class' => 'radioBtn'));
echo $this->Form->radio('gender', $options, $attributes);
How can i assign a class to the label of the option

By looking at the Form->radio() method code, nothing seems related to attributes belonging to the labels.
But to modify the display of these labels, you could use a surrounding div
echo '<div class="inline_labels">';
echo $this->Form->radio('gender', $options, $attributes);
echo '</div>';
and use CSS like this:
.inline_labels label
{
display: inline-block;
}

How about using FormHelper::label(string $fieldName, string $text, array $options)
You could define the label class in the options array, so (for example):
echo $options = array( /* relevant stuff goes here */ );
echo $attributes = array( /* relevant stuff goes here */ );
echo $this->Form->radio('gender', $options, $attributes);
echo $this->Form->label('gender', 'Text of your label', array('label'=>'radioBtn'))
Source CakePHP Cookbook on FormHelper

Work for me:
// Add your own label with CSS class
$opts = array('1' => "<label class='myCSS'>My first option</label>", "2" => "<label class='myCSS'>My second option</label>");
// Put label param to false
echo $this->Form->input('my-input', array('type' => 'radio', 'label' => false, 'options' => $opts, 'legend' => false));
Enjoy

Related

Label for Select Form Helper

I"m having trouble with CakePHP3's Form Helper class.
When I create a text input field like this:
echo $this->Form->input('fieldname');
When the input field is "required", a label appears in bold. This is fine.
However, when I create a select field like this:
echo $this->Form->select('fieldname', [1,2,3,4,5]);
The select field is created however there is no label at all. I can add it manually using:
echo $this->Form->label('fieldname');
However in cases where the select is a required field, the font is not correct (required labels show up bold with a red * following them). I've narrowed this down to the CSS only applying to nested label tags (so when I create a label tag, it's not inside a div tag like the labels for the text inputs are).
I'd like to avoid manually inserting HTML code to achieve my desired result, any help would be appreciated!
<?= $this->Form->input('fieldName', ['type' => 'select', 'options' => ['0' => 'Option1' , '1' => 'Option2'], 'empty' => __('(choose one)'), 'label' => __('LabelName')]);?>
or
$sizes = ['s' => 'Small', 'm' => 'Medium', 'l' => 'Large'];
<?= $this->Form->input('fieldName', ['type' => 'select', 'options' => $sizes, 'default' => 'm', 'label' => __('LabelName')]);?>

Big red cancel button on a cakephp form

There is a form with normal:
$this->Form->create('Users');
$this->Form->input('name');
...
that ends in
$this->Form->end('Submit');
I want to add a cancel button next to it.
$this->Html->link('Cancel', array('controller' => 'users', 'action' => 'index'))
This produces a link but I want a button.
Is there a Cake PHP way of getting this done?
There's no pure Cake way to create such a button that I know of. I think it's easiest to use the button() method of the Form helper, adding a click event to the Cancel button and just end the form without any arguments in the end() method.
$this->Form->create('Users');
echo $this->Form->input('name');
echo $this->Form->button('Submit');
echo $this->Form->button('Cancel', array(
'type' => 'button',
'onclick' => 'location.href=\'/users\''
));
echo $this->Form->end();
You can add a button like this:
$this->Form->input("", array("type"=>"button", "name" => "Cancel"));
You can add any html button attribute in the array that's in 2nd param of input.
or
$this->Form->button(
'Click me',
array()
)
);
For that you need to remove $this->Form->end('Submit'); and instead of this use this:
$this->Form->submit('Submit'); //the submit button
// put the button code i mentioned above
$this->Form->end(); // and then for closing form tag
See This link for more detail
You can make use of bootstrap button classes "btn btn-danger" for this purpose.
echo $this->Form->button('Cancel', array(
'type' => 'button',
'class' => 'btn btn-danger',
'onclick' => 'location.href=\'/users\''
));

How to update a input data by jQuery from another input selection in one form?

I am trying to transfer a value automatically from selection input area to another input area when the selection input has changed. But I cannot. Please help me. The code is here:
<?php
echo $this->Form->input('treatment',
array('options' => $types, 'id' => 'treatment_foo'));
echo $this->Form->input('fee', array('id' => 'fee_foo'');
echo $this->Js->get('#treatment_foo')->event('change',
$this->Js->request(
array(
'update' => '#fee_foo',
'dataExpression' => true,
'data' => '10'))
);
?>
Just use some JQuery
$(document).ready(function() {
$('#treatment_foo').change(function() {
$('#fee_foo').val($('#treatment_foo').val());
});
});

CakePHP: How to use HTML entities in button titles using Form Helper

I'd like to use a checkmark as the text for a button. The entity I want is ✓ but when I put that in as the text for the button using the Form Helper, it always converts the leading ampersand into & so that the text shows but not the entity.
Here's how I'm creating the button:
echo $this->Form->button(
'✓',
array(
'type' => 'submit',
'id' => $checklistItem['ChecklistItem']['id'],
'escape' => 'false'
)
);
and the generated HTML looks like this:
<button type="submit" id="1">&#x2713;</button>
which obviously doesn't render the entity.
I've tried it by setting 'escape' => 'true' but that has not effect at all.
Any ideas?
You do not need to escape it false, it is by default escaped to false.
echo $this->Form->button('✓',
array(
'type' => 'submit',
'id' => $checklistItem['ChecklistItem']['id']
)
);
You need to move your escape to the third element of the button:
echo $this->Form->button(
'✓',
array(
'type' => 'submit',
'id' => $checklistItem['ChecklistItem']['id'],
),
array('escape' => 'false')
);
Also, this has already been answered here.

Cake PHP Checkboxes

I am new to CakePHP now I'm working on checkbox I used the following statement but it
gives check box after the label and it prints the field also.My requirement is it does not
print the field name and label should be displayed after the check box.
please help me ,
Thanks in advance
<?php echo $form->input('Model.name', array('multiple' => 'checkbox', 'options' =>
$options, 'selected' => $selected));?>
First, make sure your value is a boolean or tinyint. Otherwise, you will never get a checkbox.
Then, just build like this :
echo $this->Form->input('Model.field', array(
'type' => 'select',
'multiple' => 'checkbox',
'options' => array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
)
));
My solution is according to v.2.0
<?php
echo $this->Form->input('field_name', array(
'label' => 'Some label',
'selected' => $selected
/*maybe some other options*/
));
?>
if you've specified model name above, while creating the form, you dont need to use name of model . If field is boolean, you'd get the control as checkbox automatically. Alsom you can specify it in options array like
'type'=>'checkbox'
good luck!
To draw a check box you have to first configure your table in DB properly. Set these options on your field in DB:
Field Type = Tinyint
Length/Values = 1
Set Defualt = 0
and finally your view:
echo $this->Form->input('checkbox_field');
100% will work if not then set default value for your field in view:
echo $this->Form->input('checkbox_field', array('type'=>'checkbox'));
CakePHP 3.0
$this->Form->input('id', ['type'=>'select', 'multiple' => 'checkbox', 'options'=>$array]);

Resources