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

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.

Related

Gravityforms populate selected value on dropdown within theme functions.php

I am working in gravityforms to pre-populate a form with database values dynamically, which is working. I need to be able to specity which of these options is selected by default when the form is built, but I can't find the option to do so. I have seen the placeholder, but I presume this doesnt actualy select anything on the dropdown. I have been unable to find any docs or references that allow me to set the "selected" options once I have built all the text/value pairs in the array and set the choices.
The function (which I have redacted here) works fine and populates the field, I just need to populate a placeholder and/or default selected value.
add_filter('gform_pre_render_1', 'getFieldValues');
add_filter('gform_pre_validation_1', 'getFieldValues');
add_filter('gform_pre_submission_filter_1', 'getFieldValues');
function getFieldValues($form) {
global $wpdb;
foreach ($form['fields'] as $field) {
if ($field->id == '40') {
// get the list of values from the Database
$list1Q = "SELECT <data> FROM <table> WHERE <params> = <value>";
$list1R = $wpdb->get_results($list1Q);
// Generate a nice array that Gravity Forms can understand
if (!empty($list1R)) {
$list1A[] = array();
foreach ($list1A as $listItem) {
// Add current value to the array for dropdown choices
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable);
}
}
// Set choices to field
$field->choices = $List1C;
***** THIS IS WHERE I WOULD LIKE TO SET THE SELECTED VALUE *****
}
}
return $form;
}
If there is a better way to go about populating this field, I am open to suggestions at it seems that the form loads a bit slow using this method. Otherwise, I would love to know how to set a selected choice value after populating the choices.
the quick answer is, there is an option that i found for "isSelected" that can be included in the array when defining choices. I added a ternary operation to match on what I wanted selected and it set the "selected value for me.
$isSelected = ($listItem->variable == "value I want to select") ? true : false;
$list1C[] = array('text' => $listItem->variable, 'value' => $listItem->variable, 'isSelected' => $isSelected);
Adding the ternary choice and changing the array push allowed me to set a value as selected.

How to create dynamically disable a single check box of checkboxlist in YII

In a form, I have checkboxlist which has two check box (Male and Female)
User can select both of them or any of them. And values are getting save in DB.
When we populate it from DB then I want to do like if male was selected then it should select Male checkbox and disable the Female checkbox or vice versa.
The code in my View file is:
<?php if(isset($model['gender'])){
$data = $model['gender'];
if (isset($data)) {
if($data == 0)
$htmlOptions = array(
'0' => array('label'=>'MALE'),
'1' => array('disabled'=>'disabled','label'=>'FEMALE'),);
}
if($data == 1){
$htmlOptions = array(
'0' => array('disabled'=>'disabled','label'=>'MALE', ),
'1' => array('label'=>'FEMALE'),);
}
}
echo $form->checkBoxList($model, 'gender', $htmlOptions); ?>
Problem is when I am populating it is selecting the one,I selected while saving but not disabling the other one.
For this you have to use javascript because there is no way you can handle it only with PHP. Now I don't know that much JS, but the steps are easy:
Get the checkbox id's
check which one is selected
disable the other one with innerHTML in JS.
Here you have an example on how you disable checkboxes in JS
Just so you know, the JS should be placed inside the view.

Checkbox checked using Zend Form

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);
}

CakePHP 2.x: date input with empty option/values

I have a date input and I wish to have the first option empty or blank. That way the user will have to select something, if not, validation will pick it up.
By default today's date preselected on load. I have managed to input a blank value but I cannot get it as the selected option.
echo $this->Form->input('date_of_birth', array('empty' => ''));
You should use boolean on empty attribute:
echo $this->Form->input('date_of_birth', array('empty' => true));

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.

Categories