In select drop down, make one particular value selected from values that are coming in drop down list Cakephp3.0. i am using below code:
$abc = 'india';
echo $this->Form->input('location_id',
['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false]);
Name of countries are coming in drop down list, but i want to make specific value selected which is set as variable $abc (i.e india).
Try this code :
use the 'default' key in
$abc = array('1' => 'One','2'=> 'Two');
echo $this->Form->input('location_id',
'options' => $abc,
default' =>$abc[1],
['empty' =>'Please select',
'required'=>'required',
'class' => 'form-control',
'label'=>false]
);
where $abc[0] is the key of the item you want as selected
like this :
$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('field', $options, array('default' => 'F'));
Use value option of form input helper. Like this :
$selected = 'india';
echo $this->Form->input('location_id', ['empty' =>'Please select', 'required'=>'required', 'class' => 'form-control', 'label'=>false, 'value'=> $selected]);
Suggestion - Read the docs once and then start the development. You will never get stuck at such simple problems. And refer docs first whenever you get stuck at anything.
echo $form->input('field_name', array(
'type' => 'select',
'options' => $arrayOfOptions, // typically set from $this->find('list') in controller
'label'=> 'Label name here',
'value' => $arrProjectLeaderDetails['id'], // specify default value
'escape' => false, // prevent HTML being automatically escaped
'error' => false,
'class' => 'input_select_medium'
));
In cakephp4 :
echo $this->Form->select(
'field_name', [1,2,3,4,5,6],
['empty', => '(Click to select Something']
);
Scroll down a bit from here in the docs: https://book.cakephp.org/4/en/views/helpers/form.html#options-for-select-checkbox-and-radio-controls
Related
I am attempting to create several radio buttons with a specific label...
I am using the following code, however, there is no label
echo $this->Form->radio('qualify', [['value' => 1, 'text' => 'Yes'], ['value' => 0, 'text' => 'No']], ['label' => 'Qualify Only']);
Any ideas on how I can get the label to show up for these radio buttons?
<?php
$qualify = [['value'=>'0','text'=>'No'],['value'=>'1','text'=>'Yes']];
echo $this->Form->input('qualify', array(
'options' => $qualify,
'type' => 'radio',
'placeholder' => 'Qualify Only'
));
?>
This code will be able to pick any existing value in the edit view.
You can still use the $this->Form->input and put a type whether its a select or radio
<?php
$qualify = ['0' => 'No', '1' => 'Yes'];
echo $this->Form->input('qualify', array(
'options' => $qualify,
'type' => 'radio',
'placeholder' => 'Qualify Only'
));
?>
$selected_country = array(1,3);
$this->Form->input('country', [
'options' => $source_types,
'label' => 'Country: ',
'multiple' => true,
'class' => ' form-control',
'selected' => $selected_country,
'type' => 'select'
]);
If selected country has only one value then it selects the option but if the selected country has more than one value then it doesn't select any value.
If you want to pass more than one value on $selected_country, try it:
echo $this->Form->select('rooms', [
'multiple' => true,
// options with values 1 and 3 will be selected as default
'default' => [1, 3]
]);
Reference: CakePHP Cookbook
For anyone this works
echo $this->Form->input('venues._ids', ['options' => $venues, 'class'=>'form-control select4', 'label' => false]); IF you have the database associations set up AND you have it referenced in the controller like so
$event = $this->Events->get($id, [
'contain' => ['Venues']
]);
otherwise adding the 'default' => [1, 2] ids in also works but it has to be first created in the controller and then fed to the view in list form.
I'm using CakePHP Form helper to create form input fields. When a user edit an item, it will pre-fill the fields with data. I use v-model on each input field to make sure data is correctly populated.
It's all good for the textboxes as data is bound correctly. But for <select> fields, v-model doesn't seem to work. I've tried using default property for the Form->input() but no help. Here's some code:
<div class="fields">
<?= $this->Form->input('email', [
'label' => __('Email'),
'type' => 'email',
'v-model' => 'emailModel',
'placeholder' => __('Required'),
'#keydown.enter.stop.prevent' => 'return false;',
'#keyup.enter.stop.prevent' => 'onSubmit()'
]); ?>
<?= $this->Form->input('status', [
'label' => __('Status'),
'type' => 'select',
'default' => '{{userStatus}}',
'options' => [
1 => 'Active',
2 => 'Inactive'
],
'#keydown.enter.stop.prevent' => 'return false;',
'#keyup.enter.stop.prevent' => 'onSubmit()'
]); ?>
</div>
The email textbox has correct data filled, but not for the status selectbox. But if I replace {{userStatus}} with, for example, 2, it will set the default option to Inactive, even though {{userStatus}} itself has value 2.
How do I go about solving this?
I am new to VueJS but I came across the same question. The VueJS documentation states:
v-model will ignore the initial value, checked or selected attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the data option of your component.
https://v2.vuejs.org/v2/guide/forms.html#Basic-Usage
Therefore, you need to assign your data.userStatus with your default value.
var vm = new Vue({
el: '#app',
data: {
statusModel: 2 // The default value goes here
}
});
PHP Side:
<div class="fields">
<?= $this->Form->input('email', [
'label' => __('Email'),
'type' => 'email',
'v-model' => 'emailModel',
'placeholder' => __('Required'),
'#keydown.enter.stop.prevent' => 'return false;',
'#keyup.enter.stop.prevent' => 'onSubmit()'
]); ?>
<?= $this->Form->input('status', [
'label' => __('Status'),
'type' => 'select',
'v-model' => 'statusModel',
'options' => [
1 => 'Active',
2 => 'Inactive'
],
'#keydown.enter.stop.prevent' => 'return false;',
'#keyup.enter.stop.prevent' => 'onSubmit()'
]); ?>
</div>
Hope this helps.
I need to modify some code in cakePHP and the last thing that is holding me still is my inability to get current value from input form and send it to my jquery script.
The forms look like this:
<?php echo $this->Form->input('country', array('options' => $countriesOption, 'onchange' => 'getHotels(this.value, $(\'showSingleInput\').checked);', 'style' => 'margin: 10px;', 'div' => false, 'label' => false, 'error' => 'false', 'hiddenField' => false, 'id' => 'countryInput')); ?>
<?php
if($regionOption != null){
echo $this->Form->input('region', array('options' => $regionOption, 'onchange' => 'getHotelsInRegion(this.value,this.value, $(\'showSingleInput\').checked);', 'style' => 'margin: 10px;', 'div' => false, 'label' => false, 'error' => 'false', 'hiddenField' => false, 'id' => 'regionInput'));
}?>
The problems is at second input form 'region'.
I need to pass as first value to JQuery method currently selected value from 'country' form input.
And all the hard stuff I need to do is here:
'getHotelsInRegion(this.value,this.value, $(\'showSingleInput\').checked);'
Something like
$('#ModelNameFieldName').change(function() {
alert($(this).val());
});
should to the trick.
I am adding a select element to a Zend_Form instance as follows:
$user = $form->createElement('select','user')->setLabel('User: ')->setRequired(true);
foreach($users as $u)
{
if($selected == $u->id)
{
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
//*** some way of setting a selected option? selected="selected"
}
else
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
}
I have been searching the docs but cannot find a simple way of pre-setting an option of the select element to 'selected'.
I've just worked out how to do it.
You have to use the setValue() method of the element:
$user = $form->createElement('select','user')->setLabel('User: ')->setRequired(true);
foreach($users as $u)
$user->addMultiOption($u->id,$u->firstname.' '.$u->lastname);
$user->setValue($selected); //$selected is the 'value' of the <option> that you want to apply selected="selected" to.
$form->addElement('select','foo',
array(
'label' => 'ComboBox (select)',
'value' => 'blue',
'multiOptions' => array(
'red' => 'Rouge',
'blue' => 'Bleu',
'white' => 'Blanc',
),
)
);
As above, you can use 'value' => 'blue' for making 'blue' => 'Bleu' selected.
I hope this will help you..
In Zend Framework 2 set the 'value' attribue. For example to default the Select to 'Yes':
$this->add( array(
'name' => 'isFlexible',
'type' => 'Select',
'options' => array(
'label' => 'Is it flexible?'
,'label_attributes' => array( 'placement' => 'APPEND')
,'value_options' => array(
'' => 'Select Below',
'0' => 'No',
'1' => 'Yes',
'2' => 'N/A',
),
),
'attributes' => array(
'id' => 'is_flexible',
'value' => 1,
),
));
i think this should work:
$form->setDefault('user', 'value'); // Set default value for element
To set default values you can try both setDefault or populate
$form->populate( $array_keypair_values );
I just try following code to show drop-down value as selected from controller and it work fine.
$user->setValue($value); //$value is the 'value' of the and $user is the element of from.
The mentioned solutions won't work for Zend Framework 2, For those who use Zf2, I suggest using the following instruction to set a default value:
$formX->get('<Select element Name>')->setValue(<the id of the selected item>);