CakePHP 3 - Set checkbox to checked in edit view - cakephp

maybe I'm blind on the point but I'm struggling for over an hour and can not find a solution.
My goal is in edit view a checkbox, which is set in the database to "yes", also to be displayed as checked.
My InputField looks like this:
<?php
echo $this->Form->checkbox(
'active',[
'value'=>'yes',
'hiddenField'=>'no'
]
);
?>
Saving the values, works.
But if the value "Yes" is saved, I call the edit view again, the checkbox is not marked as checked.
Is there any way to get around this? Would be more pleasant for the corresponding user as in the remaining input fields to get the value issued.
Do you have an idea or an approach?
Thank you in advance.

I think should be
echo $this->Form->checkbox('active', [
'checked' => $your_table->active == 'yes' ? true : false,
]);
Don't forget to use $this->set(compact('your_model')); of edited record.

Related

How to create true or false checkbox in Yii2?

I have a problem here. In my ActiveForm I need to create a simple checkbox (which has to be a boolean value (0 - if the item was unpaid and 1 - if it was paid).
I've created in my view file a checkbox like this:
<?= $form->field($model, 'sign')->checkboxList([
'' => '',
]); ?>
I thought that if I will mark the checkbox it will automatically will change to 1 (because in the database the value was set was boolean), but now it's writing that it must be an integer. When I'm deleting my rule and submitting my form, that value in the database is NULL.
So could anyone tell me how to do a checkbox, which if I would mark it, the value in the database would be 1, otherwise - 0? Thank you for any help..
If it is only 1 option, instead of checkboxlist, try checkbox:
$form->field($model, 'sign')->checkbox();

Best way to handle checkbox in Laravel4?

I'm starting to work with Laravel4, I was struggling with some basic stuff like this one below. There's any other way to handle the value for an unmarked checkbox than adding a hidden checkbox for the default value like below?
<td class="text-center">
{{Form::hidden('status', 0)}}
{{Form::checkbox('status')}}
</td>
This is working fine for me right now, but I would like to know if there's any better way like handling the value in the Controller#update.
EDIT: The values of the checkbox on the form are being handle by Input::all() at the Update action.
Thanks for the feedback.
This is default behavior of HTML actually, not related to how Laravel handle inputs. From HTML spec:
When a form is submitted, only "on" checkbox controls can become successful.
...
Every successful control has its control name paired with its current value as part of the submitted form data set.
So, a more generic approach would be not to change your HTML form, but change the behavior when you are retrieving the input, e.g.:
$status = Input::get('status', false);
In the FormBuilder class, the checkbox method has these parameters.
checkbox(string $name, mixed $value = 1, bool $checked = null, array $options = array())
So you want:
Form::checkbox('status', 0, false, ['class' => 'form-control'])
Happy coding!

Saving a Disabled field

In my edit.ctp I have a select box which I don't need the user to change. So I put array(disabled=>true). But this field is not coming when
pr($this->data); and showing an error while saving.
What options are there to solve this issue.
If you know the value of the data you can edit it at the controller.
$this->request->data['ModelName']['fieldName'] = value;
UPDATE
Edit it like
echo $this->Form->input('patient_id',array('type'=>'select', 'readonly' => 'readonly'));
You could make the field readonly so that user cant change it , or use some hidden field to post the data that you want, or you could use some css, like visibility:hidden, so that user dont see it but it'll be posted.
echo this->Form->input('patient_id',array('type'=>'hidden'));
You can use some other name for the input and check in controller, or you could completely remove the select element from the view*strong text* (since, its not needed as user dont need to change it)

Cakephp - keep selected value for dropdownlist after submit

How can I keep the selected value for a dropdownlist after form submission in Cakephp?
If more info (or some code) is needed just tell me please.
UPDATE
Here is part of the code in my view:
echo $this->Form->create('Chart');
echo $this->Form->input('username',
array('label'=>('Usernames List'),
'default'=>('Select username'),
'options'=>$usernames, 'selected'=>false));
echo $this->Form->end('Create Chart');
So, when I press 'Create Chart', the dropdownlist doesn't keep the username that I selected, but it goes back to the first one.
The Form helper uses the data stored in $this->data to prepopulate fields. Make sure that when you are submitting the form, the view that is rendered after has the appropriate model/key data stored in $this->data in order for the Form helper to correctly fill in the appropriate values.
Can we see your controller action possibly? That may help draw a more accurate conclusion.
you should never use the view to set defaults or values (especially selected/value is wrong as it - like your code - destroys the idea of persistent forms).
use the controller instead
#see http://www.dereuromark.de/2010/06/23/working-with-forms/ (Default Values)
add value in dropdown like this:
<?php echo $this->form->select('Schedule.showsid', array('0'=>'title', '1'=>'description'));?>

CakePHP form helper - change value of hidden input for checkbox/radio

Using CakePHP's form helper to generate a checkbox is easy enough; to use the example from the documentation:
echo $this->Form->checkbox('done',array('value' => 555));
This will produce the following HTML:
<input type="hidden" name="data[User][done]" value="0" id="UserDone_" />
<input type="checkbox" name="data[User][done]" value="555" id="UserDone" />
This is all well and good, and the hidden field serves to force submission of a value for the "done" field even if the box remains unchecked.
Now, for the sake of argument, let's say the database definition of this field is ENUM('yes','no'). Of course I can easily change the value of the checkbox to "yes". However, if it's unchecked, a value of "0" is submitted from the hidden element. This produces no error or warning from mysql, as 0 is always a legal value for an enum field; it appears as an empty string.
Can I change value of the hidden field that CakePHP generates (to "no"), or do I need to suppress the auto-generation and create the hidden field myself? (An annoyance that grows with the number of checkboxes.)
I believe this all applies to radio button groups, too—at least if they don't have a default selection.
I'm using CakePHP 1.3. Thanks.
With FormHelper::checkbox, you can use hiddenField to set the default value.
<?php echo $this->Form->checkbox('done', array('value'=>'yes', 'hiddenField'=>'no');?>
With FormHelper::radio, you can only set value to default to one of the options, if the values match. This will also suppress the hidden field.
<?php echo $this->Form->radio('done', array('yes' => __('Yes')), 'no' => __('No'), array('value'=>'no');?>
Also, you should remember that CakePHP does not support enums (and I am sure this sort of scenario is one reason)
If your field data is truly binary (yes/no true/false enables/disabled etc.) then for the sake of CakePHP conventions you should just use an int(1) or tinyint(1) field and then convert the boolean value to yes/no etc in the view.
Then you don't have to worry about creating your own hidden input values and disabling the generated hidden inputs.
Another option would be to override the form->helper checkbox method that gets called by form->input to accept a new key in the options array that sets the value to something other than a 0 / false.
Unfortunately, FormHelper::checkbox allows you to disable the hidden element, but not to select its value, so you will need to do so and create the hidden field yourself. For example:
<?php echo $this->Form->hidden('done',array('value'=>'no'))?>
<?php echo $this->Form->checkbox('done',array('value'=>'yes','hiddenField'=>false))?>
With FormHelper::Radio worked for me like that
echo $this->Form->radio(
'done',
['yes' => __('Yes'), 'no' => __('No')],
['hiddenField' => false]
);

Resources