Checkbox checked using Zend Form - php

IN this way checkbox is created
$is_conveyance_required = new Zend_Form_Element_Checkbox(FORM_CHECKBOX_PREFIX . 'is_conveyance_required', array());
$is_conveyance_required->addDecorators(array(
array('HtmlTag', array('tag' => 'label')),
array('Label', array('tag' => '')),
));
$is_conveyance_required->setValue(1);
$is_conveyance_required->setChecked( true );
$this->addElement($is_conveyance_required);
and how form populating
$personal_form->populate($personal_data);
But zend form not populating checkbox...
<input type="checkbox" value="1" id="chk_is_conveyance_required" name="chk_is_conveyance_required">
Here is $personal_data array img

It's simply not working how you expect it to operate.
From the Zend Manual:
By default, the checked value is '1', and the unchecked value '0'. You
can specify the values to use using the setCheckedValue() and
setUncheckedValue() accessors, respectively. Additionally, setting the
value sets the checked property of the checkbox. You can query this
using isChecked() or simply accessing the property. Using the
setChecked($flag) method will both set the state of the flag as well
as set the appropriate checked or unchecked value in the element.
Please use this method when setting the checked state of a checkbox
element to ensure the value is set properly.
You could do some workaround by using something like this (untested!) in you controller as addition to popuplate()
if($personal_form->is_conveyance_required->getValue() == 1) {
$personal_form->is_conveyance_required->setChecked(true);
}

Related

How to submit unchecked checkboxes to controller

Ok first I have a "settings" table on my database in which I have the fields "name" and "value" its a configuration kind of table where the value could be anything from string to boolean values etc.
Now on my blade, I have a form with various inputs "texts" "selects" "checkboxes" etc. When submitting the form on the controller I run a foreach where for each attribute of the $request I store its key as the name and its value as its value on the database.
$agency_id = Auth::user()->agency->id;
$settings = AgencySettings::whereAgencyId($agency_id)->get();
foreach ($request->except('_token') as $key => $value)
{
$setting = $settings->where('name','=',$key)->first();
if (boolval($setting))
{
$setting->value = $value;
$setting->update();
}else{
$setting = new AgencySettings;
$setting->agency_id = $agency_id;
$setting->name = $key;
$setting->value = $value;
$setting->save();
}
}
All works well except the unchecked checkboxes which are not inside the $request.
I know I can handle them like so $request->has('name_of_checkbox') but because of the dynamic nature of the table on the database, I don't want to have hardcoded on my Controller the name of a specific setting.
My goal is that the code on my Controller will be the same regardless the number of different settings I will use on my frontend (maybe in the future there will be a need to add more).
So my question, is there a way to handle those checkboxes serverside without having to refer to them specifically, or a way to always return the value of the checkboxes to the server despite its state?
My first thought is to go with javascript and hidden inputs, but maybe there is a better way.
You could add a hidden field with the same name before every checkbox you want to receive, like :
<input type="hidden" name="checkbox-1" value="0" />
<input type="checkbox" name="checkbox-1" value="1" /> My checbox 1
That will send the hidden field with the 0 value when the field is unchecked and send the proper truthy value when it's checked.
NOTE: Just make sure you're adding the hidden field first so you'll receive just the checked one when the field is checked.
Other solution is to simply check if value for "checkbox-1" is set in post array.
So you will set default to 0 on your controller side and check if value exists instead of checking if it is 0 or 1.
(M.)

Symfony always saves checkboxes as zero

On Symfony 1.4.11 I have a boolean field that is set on a form by a HTML checkbox. When the value is 0 (unchecked) then the HTML of the checkbox is
<input type="checkbox" value="" name="gift_type[valid]">
When I try and save the from it is always saved as zero, I assume because the input has no value.
The code I am using for the checkbox is generated by the symfony admin generator so I thought it would just work "out of the box". If I uncheck a true value then that works as expected.
schema.yml
GiftType:
columns:
valid: { type: boolean, notnull: true, default: true }
BaseGiftTypeForm.class.php
$this->setWidgets(array(
'valid' => new sfWidgetFormInputCheckbox()
));
$this->setValidators(array(
'valid' => new sfValidatorBoolean(array('required' => false))
));
Your HTML checkbox looks funny,
are you sure it's been generated by symfony? (the id is missing i.e.)
<input type="checkbox" name="gift_type[valid]" checked="checked" id="gift_type_valid" />
Anyway all the code you show is OK, there is no need to change the schema type! (ping #richrosa).
sfValidatorBoolean return a boolean value, then it's passed to the object, and then the object is saved. You have to debug your model. Out of the box, a boolean field in the Doctrine Admin Generator work perfectly fine.
You can add this code in your GiftTypeForm class to help debug:
protected function doUpdateObject($values)
{
var_dump($values);die();
$this->getObject()->fromArray($values);
}
If you don't see the "valid" key boolean, there is a validation process error, if it's here, you have a model issue (have you overwrite the save method?).
I'm not aware of how Symfony works. In plain PHP/HTML the value of the checkbox is independent of whether it is checked or unchecked. If a checkbox is checked it's value will be submitted in the form post; if it is unchecked it won't be submitted.
Try changing your datatype to tinyint.
valid: { phpName: Valid, type: TINYINT, size: '4', required: false }
Here is a checkbox with a default value and a label.
$this->setWidgets(array(
'valid' => new sfWidgetFormInputCheckbox(array('label'=>'My Label') , array('value' => '1'));

PHP CodeIgniter - Dropdown using foreach does not show "selected" value

I am generating options for my dropdown using
// view.php
foreach ($plants as $row):
$options[$row->plant_id] = $row->plant_name;
endforeach;
and then lower in the HTML part of view.php
//view.php
$js = 'onChange = "plantDateDelete(\'/size/get_dates_for_plant/\'+this.value);"';
echo form_dropdown('plant_id', $options, 'Select', $js);
The dropdown show options OK, but it does NOT show 'Select' as the "selected"/default value. It shows up with the first option of the array instead.
The HTML source also shows 'Select' in form_dropdown was ignored.
I really need this dropdown to show up with 'Select' as default so as to force the user to activate the onChange function.
Any idea what is going on here or how to solve this issue?
You need to have the default option in your $options array...
So before your foreach, do:
$options = array('Select');
I should add, this will have the value of 0 in the dropdown, but as its the first element in the array it will be selected by default, unless another option is passed as the default value.
If you wanted to explicitly set this value as the default you would pass 0 as the default argument.

To create check box in symfony using widgets and also validate it?

the code in symfony that i am using,
$this->setWidgets(array(
'mobile' =>new sfWidgetFormInput(),
'subscribetosms' =>new sfWidgetFormInputCheckbox(),
));
i want to validate the checkbox, and also code to take values from check box
to validate form fields in symfony u need to set validators like this (assuming you are in a form class):
$this->setValidators(array(
'mobile' => new sfValidatorString(array(...)),
'subscribetosms' => new sfValidatorInteger(array(...))
));
Question is, what do you want to validate? If you want some kind of value send to your php script if the checkbox is selected you need to set this value in the widget.
new sfWidgetFormInputCheckbox(array('value_attribute_value'=>'your_value' )
Now you could configure your validator to validate this value (sfValidatorString for a string, of sfValidatorInteger for an integer).
To get the value in your action after the validation:
if ($this->form->isValid()) {
$myValue = $this->form->getValue('subscribetosms');
}

how to set an option for form->input( 'multiple'=>'checkbox')

i plan to set a checkbox with selected option in my form.
but i am unable to show my checkbox content in the form, i cant see any value instead of just a box for me to select.
how to show value while i using checkbox? i able to show my value while i using select.
this is in a HABTM model. any hints?
here is my selection code.
input('User',array('label'
=> 'Select Related Potential',
'multiple'=>'checkbox',
//'options' => $users,
'legend'=>$users,
//'value'=>$users,
//'id'=>$ownUserId,
'default'=>$ownUserId,
'style'=>'width:200px;height:100px',
'selected' => $ownUserId, )); ?>
This may be relevant:
You cannot use default to check a checkbox - instead you might set the value in $this->data in your controller, $form->data in your view, or set the input option checked to true.
For example:
// in Controller
$this->data['Model']['field'] = true;
Causes the field to have the value true, which will result in a checked checkbox for this field.
Or:
$form->input('Model.field', array('checked' => true));
Always checks the checkbox.
Or:
$form->input('Model.field', array(
'checked' => ($this->data['Model']['field'] == 'xxx')
));
Dynamically sets the checkbox based on whether $this->data['Model']['field'] is 'xxx' or not.
Sorry, completely misunderstood the question.
Did you find your users via list? The options array needs to be in a particular format, a normal find() won't do that.

Categories