How to use check a checkbox in CodeIgniter? - php

I have checkbox which is created as below in CI:
echo form_checkbox(array(
'name' => 'active',
'id' => 'active',
'value' => '1'
));
I also have other fields such as Name which have a required field validation applied. So, when I leave Name as blank and tick the active checkbox, then it shows error for name. But it removes the check on active checkbox. I want to keep the active checkbox as checked.
I know about a set_checkbox method in CI, but not sure how to use this in the above. In case of form_input we simply use set_value and all is ok. But with set_checkbox it returns full string checked="checked". So when I use the set_checkbox and form_checkbox as combined as below, then it doesn't work.
echo form_checkbox(array(
'name' => 'active',
'id' => 'active',
'value' => '1',
'checked' => set_checkbox('active', '1')
));
Any idea how to fix this?

You can set this as
echo form_checkbox(array(
'name' => 'active',
'id' => 'active',
'value' => '1',
'checked' => ($this->input->post('active') && $this->input->post('active') == 1 )
));
IF you Do not use ($this->input->post('active') && ....) in your condition it will throw an error if you do not check this check-box and submit the form.

The checked attribute on form_checkbox accepts TRUE/FALSE value. A simple comparison to see if the value is set will work.
echo form_checkbox(array(
'name' => 'active',
'id' => 'active',
'value' => '1',
'checked' => ( $this->input->post('active') == 1 )
));
The set_checkbox method was not made to be used in this style. It's intended to be use inside existing HTML as per this example from the CI User Guide
<input type="checkbox" name="mycheck" value="1" <?php echo set_checkbox('mycheck', '1'); ?> />
<input type="checkbox" name="mycheck" value="2" <?php echo set_checkbox('mycheck', '2'); ?> />

Related

Yii: Make dropDownList readonly/disabled in form

I am trying to make DropDownList readonly or disable in form for update condition but my code is not working ...
echo $form->dropDownListRow($model, 'sem_id', CHtml::listData(Sem::model()->findAll(), 'id', 'title'/*, array("disabled" => "disabled")*/ , array('readonly' => 'readonly') ));
Error: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members
First thing is you should dropDownList instead of dropDownListRow. Also you forgot to use ) after 'title' And you should use "disabled" => "disabled" instead of 'readonly' => 'readonly'. I don't think they have a readonly attribute in html tags.
echo $form->dropDownList($model, 'sem_id', CHtml::listData(Sem::model()->findAll(), 'id', 'title'), array("disabled" => "disabled"));
This can make disabled dropdown list, but note that you can't relay on html attributes. Because the users can remove this attribute easily and can change dropdown value. So you need to have a control on your server.
Working example of Yii 2 dropdown disable:
<?=
$form->field($model, 'customer_status')->dropDownList([
'0' => 'Requested',
'1' => 'Registered',
'3' => 'Un-Rgistered',
'2' => 'Disabled'
], ['disabled' => 'disabled'])
?>

yii2: make checkbox to be checked

I am using Yii2 framework and I'd like to generate an html code like this
<input type="checkbox" id="queue-order" name="Queue[order]" value="1" checked>
in a view which uses ActiveForm.
I've tried
echo $form->field($model, 'order')
->checkBox(['label' => ..., 'uncheck' => null, 'checked' => true]);
as well as
echo $form->field($model, 'order')
->checkBox(['label' => ..., 'uncheck' => null, 'checked' => 'checked']);
but desired string "checked" does not appear in the generated html code.
Strangely enough, if I substitute "checked" with "selected"
echo $form->field($model, 'order')
->checkBox(['label' => ..., 'uncheck' => null, 'selected' => true]);
then generated html code contains attribute "selected":
<input type="checkbox" id="queue-order" name="Queue[order]" value="1" selected>
So, how can I generate html code for a checkbox with attribute "checked"?
I guess this checkbox will be checked only if $model->order property take true value and if it has false (0 or null or false etc) value - field will be unchecked.
if your are setting an external value in checkbox.
<?php $model->order = "02256"; ?>
<?= $form->field($model, "order")->checkbox(['value' => "02256"]); ?>
echo $form->field($model, 'Status')->checkbox(['uncheck' => 'Disabled', 'value' => 'Active']);
You can use checked attribute to mark it as checked
<?= $form->field($model, 'agent_email_verification')->checkbox(['checked' => $model->agent_email_verification > 0, 'value' => true]) ?>

how do i assign a class to the checkbox

I have a piece of code which works perfectly the way it has been written. It produces a checkbox when viewed on a website.
<?=form_checkbox('heart_rate', 'yes', sel_checkbox($admission['heart_rate'], $observation[0]->heart_rate))?> <?=form_label('Heart Rate')?>
I would like to know how to give the checkbox a class called mainobservations without altering the other properties.
i tried
<?php $data = array('class'=> 'mainobservations' );?>
<?=form_checkbox($data,'heart_rate', 'yes', sel_checkbox($admission['heart_rate'], $observation[0]->heart_rate))?> <?=form_label('Heart Rate')?>
but this did not work so any suggestions on how to arrange the code?
You must set the other values as well as the class in an array and use that:
$data = array(
'name' => 'heart_rate',
'class' => 'mainobservations',
'value' => 'yes',
'checked' => sel_checkbox($admission['heart_rate'], $observation[0]->heart_rate)
);
form_checkbox($data);
http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html

Cannot catch value from form_hidden on codeigniter?

I try to create Update Form with form helper CI. everything Work if on form_input. But, when id on form_hidden, it return NULL. this the script
on View
$hidden = array('name'=>'id_hidden','value'=>$datacompany[0]->id);
echo form_hidden($hidden); //I have edited
On Controller
function edit_company()
{
if(isset($_POST['EDIT']))
{
print_r($_POST);//return All value
$isi = array(
'id' =>$this->input->post('id_hidden'),//return null
'nip' =>$this->input->post('nip'),//return value
'nama' =>$this->input->post('nama'), //return value
'golongan' =>$this->input->post('golongan') //return value
);
echo $isi['id']; //the result id is null
}//end if
}//end Function
That Id I need for using on model. How Can I Fix That?
How to Catch ID from form_hidden?
I'm very appreciated Your Answer
thanks
While using my first comment will let you debug it in 3 seconds, Here's the answer :)
You're using the form_hidden in the wrong way.
An array in the form_hidden turns to this (from the docs)
$data = array(
'name' => 'John Doe',
'email' => 'john#example.com',
'url' => 'http://example.com'
);
echo form_hidden($data);
// Would produce:
<input type="hidden" name="name" value="John Doe" />
<input type="hidden" name="email" value="john#example.com" />
<input type="hidden" name="url" value="http://example.com" />
As you see, the 'keys' of the array turn to the names of the fields.
The value is the 'value' of the array. so in your example, you're making two hidden fields.
<input type="hidden" name="name" value="id_hidden">
<input type="hidden" name="value" value="$datacompany[0]->id">
You need to define like this a hidden field in CI:
$hidden = array('id_hidden',$datacompany[0]->id); // a name and value pair for a single instance.
echo form_hidden($hidden);
$hidden = array('id_hidden' => $datacompany[0]->id);
echo form_hidden($hidden);
I think this will fulfill your need.
Or if you want other attributes... Try with this..
$data = array(
'name' => 'username',
'id' => 'username',
'value' => 'johndoe',
'maxlength' => '100',
'type' => 'hidden',
'size' => '50',
'style' => 'width:50%',
);
echo form_input($data);
Depend on your need.

cakephp generates wrong label for radio input id

When i create a form on CakePHP with radio inputs, the label generated not match with the id of the radio input, the label "for" duplicates the name of the form. Here is my code:
echo $this->Form->create(
'test',
array(
'action' => 'index',
'type' => 'post',
'class' => 'fill-up',
'inputDefaults' => array('div' => 'input')));
$options = array('option1' => '1',
'option2' => '2');
$attributes = array('legend' = > false);
echo $this->Form->radio('Type', $options, $attributes);
echo $this->Form->end(
array(
'label' = > 'end',
'class' = > 'button',
'div' = > false));
and the generated HTML is something like:
<input type="hidden" name="data[test][options]" id="testOptions_" value="">
<input type="radio" name="data[test][options]" id="TestOptionsOption1" value="option1">
<label for="testTestOptionsOption1">1</label>
<input type="radio" name="data[test][options]" id="TestOptionsOption2" value="option2">
<label for="testTestOptionsOption2">2</label>
as you can see, cake duplicate the form name "test" on the label. How I can fix this? I try with the exact code of the documentations and still have the same issue
hope you can help me, thx very much
I auto-answer my question. That was a bug of cakephp, solved on the last version:
https://github.com/cakephp/cakephp/releases/tag/2.4.2
Try using
'label' => array(
'class' => 'thingy',
'text' => 'The User Alias'
)

Categories