Is that possible to make gridview dropdownlist set value as default value? For example my working status has 2 type which is active and no active. Possible set active as default value?
the code as below:
array(
'name'=>'employee_wstatus',
'type' => 'raw',
'filter'=>array('1'=>'Active', '0'=>'No Active'),
'value' => 'EmployeeM::model()->getWorkstatus($data->employee_wstatus);',
'htmlOptions'=> array('width'=>'10%'),
),
If you look at the implementation of Yii filter method, you will see, that it always adds empty option when it gets an array on input.
To remove blank value from your dropdown filter you have to define selectbox manually:
'filter' => CHtml::activeDropDownList(
EmployeeM::model(),
'employee_wstatus',
array('1' => 'Active', '0' => 'No Active')
),
Related
Please help... I am trying to set default value for this: I know it is based on Kartik select on yii2. I didn't use it before. Here is my source code I would need to set default value based on $_GET parameters. But problem is I can't set any. The source is this...
<?php
echo $form->field($profile, 'country_id')->widget(Select2::classname(), [
'language' => Yii::$app->language,
'data' => ArrayHelper::map($countries, 'id', 'title_' . mb_substr(Yii::$app->language, 0, 2)),
'theme' => Select2::THEME_BOOTSTRAP,
'options' => [
'id' => 'country-select',
'placeholder' => Yii::t('frontend', 'Select a country')],
'pluginOptions' => [
'allowClear' => true,
],
])->label($Country, ['class' => 'label-class'])
?>
Where Should I set it in this case. Sorry, I just saw this plugin at first time...
if you wanna set the default value, you should use the model like the following code;
$profile->country_id = isset($profile->country_id) ? $profile->country_id : 1 // like that
also you can use in afterFind function on the model.
check this Yii2: How to set default attribute values in ActiveRecord? and this https://www.yiiframework.com/doc/guide/2.0/en/tutorial-core-validators#default
I need to populate some entity value into the radio HTML code, I look
through all documentation but does not seem to be able to find any help
below is the php code, {{entity->description}} is the entity value I want to show
$this->Form->setTemplates ( [
'radioContainer' => '{{content}}'
] );
echo $this->Form->control ( 'OrderShippingMethodId',
[
'templates' => [
'nestingLabel' => '**<p>{{entity->description}}</p>**
{{input}}{{text}}',
'radioWrapper' => '{{label}}' ],
'options' => $shippingMethodsRadio,
'type' => 'radio',
'label' => false,
'type' => 'radio',
'id' => 'OrderShippingMethodId',
'legend' => false ,
'escape' => false
] );
it generate the below html code, the <p></p> is not showing the entity description value
Shipping
as can be seen in the bold code above, the entity->description value cannot be read
out, seem that the only value the form->control can read out from the
entity is text, value. is there anyway to access other value in the entity
in the controller method, to populate $shippingMethodsRadio
$this->set('shippingMethodsRadio' => $shippingMethods->extract('radio'));
the $shippingMethods, will contain a collection of the entity shippingMethod, I want to read other value like description property in the entity into the radio control, the radio property in the shippingMethod return an array contains value, text, description property of the entity to populate the radio control
thank you
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'])
?>
I create 2 arrays from a model, 1 being already selected values from the user, and the 2nd available values which the user can then also select. This is for an edit page. I want to populate a multi-select input box with the values of both models, but want the already chosen values (1st array) highlighted. It creates the models fine, and using array_merge() I merge both the arrays as the options, but the selected does not highlight the correct fields. Any tips?
// Controller:
$ivrNumbersAvail = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array($id)))));
$ivrNumbersSelected = $this->Survey->SurveyIvrNumber->find("list",array("conditions" => array("OR" => array("SurveyIvrNumber.survey_id" => array(0)))));
// In the view:
echo $this->Form->input('SurveyIvrNumbers.id',array(
'empty' => '-- Select IVR Number(s) --',
'options' => array_merge($ivrNumbersAvail,$ivrNumbersSelected),
'selected' => $ivrNumbersSelected,
'class' => 'textbox',
'multiple' => true,
'div' => array(
'class' => 'field'
),
'label' => array(
'class' => 'label-tooltip',
'title' => '', //tool tips
'text' => 'IVR Numbers: (you can select multiple numbers)'
),
'after' => '<p class="field-help"></p>'
));
If you set $this->request->data to the record you are currently editing CakePHP will automatically populate this data for you!
// CONTROLLER
// this line sets the data
$this->request->data = $this->Survey->read(null, $id);
// this passes the SurveyIvrNumbers to the view, (you can put any options on to this)
$this->set('SurveyIvrNumber',$this->Survey->SurveyIvrNumber->find('all'));
// VIEW
// CakePHP does the rest
echo $this->Form->input('SurveyIvrNumbers',array(
'empty' => '-- Select IVR Number(s) --', // plus other options
);
Using CakePHP:
I have a many-to-one relationship, let's pretend it's many Leafs to Trees. Of course, I baked a form to add a Leaf to a Tree, and you can specify which Tree it is with a drop-down box ( tag) created by the form helper.
The only thing is, the SELECT box always defaults to Tree #1, but I would like it to default to the Tree it's being added to:
For example, calling example.com/leaf/add/5 would bring up the interface to add a new Leaf to Tree #5. The dropdown box for Leaf.tree_id would default to "Tree 5", instead of "Tree 1" that it currently defaults to.
What do I need to put in my Leaf controller and Leaf view/add.ctp to do this?
In CakePHP 1.3, use 'default'=>value to select the default value in a select input:
$this->Form->input('Leaf.id', array('type'=>'select', 'label'=>'Leaf', 'options'=>$leafs, 'default'=>'3'));
You should never use select(), or text(), or radio() etc.; it's terrible practice. You should use input():
$form->input('tree_id', array('options' => $trees));
Then in the controller:
$this->data['Leaf']['tree_id'] = $id;
$this->Form->input('Leaf.id', array(
'type'=>'select',
'label'=>'Leaf',
'options'=>$leafs,
'value'=>2
));
This will select default second index position value from list of option in $leafs.
the third parameter should be like array('selected' =>value)
Assuming you are using form helper to generate the form:
select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)
Set the third parameter to set the selected option.
cakephp version >= 3.6
echo $this->Form->control('field_name', ['type' => 'select', 'options' => $departments, 'default' => 'your value']);
To make a text default in a select box use the $form->select() method. Here is how you do it.
$options = array('m'=>'Male','f'=>'Female','n'=>'neutral');
$form->select('Model.name',$options,'f');
The above code will select Female in the list box by default.
Keep baking...
FormHelper::select(string $fieldName, array $options,
array $attributes)
$attributes['value'] to set which value should be selected default
<?php echo $this->Form->select('status', $list, array(
'empty' => false,
'value' => 1)
); ?>
If you are using cakephp version 3.0 and above, then you can add default value in select input using empty attribute as given in below example.
echo $this->Form->input('category_id', ['options'=>$categories,'empty'=>'Choose']);
The best answer to this could be
Don't use selct for this job use input instead
like this
echo $this->Form->input('field_name', array(
'type' => 'select',
'options' => $options_arr,
'label' => 'label here',
'value' => $id, // default value
'escape' => false, // prevent HTML being automatically escaped
'error' => false,
'class' => 'form-control' // custom class you want to enter
));
Hope it helps.
As in CakePHP 4.2 the correct syntax for a select form element with a default value is quite simple:
echo $this->Form->select(
'fieldname',
['value1',
'value2',
'value3'],
['empty' => '(auswählen)','default'=>1]
);
If hopefully don't need to explain the default=1 means the second value and default=0 means the first value. ;)
Be very careful with select values as it can get a little tricky. The example above is without specific values for the select fields, so its values get numerated automatically. If you set a specific value for each select list entry, and you want a default one, set its specific value:
$sizes = ['s' => 'Small',
'm' => 'Medium',
'l' => 'Large'];
echo $this->Form->select('size', $sizes, ['default' => 'm']);
This example is from the official 4.x Strawberry Cookbook.
https://book.cakephp.org/4/en/views/helpers/form.html#options-for-control