How select a select input type in phalcon form? - phalcon

I have a select type in phalcon form
$category = new Select('category', Category::findForSelect(), [
'useEmpty' => true,
'emptyText' => 'Select...',
'emptyValue' => ''
]);
It works on new form. When i edit the form with old data the category does not get selected on edit.

Related

Yii 2.0 Select Pre-selected values from Database

I have been trying to fix an issue but to no avail but i am sure i will find a solution here. I am using Kartik 2.0 Select extension to do a multiple select. Fine, all working when inserting into the database but i am unable to retrieve the saved records to be displayed as selected back in the select field.
//I have included the kartik widgets already
use kartik\widgets\Select2;
<label>Desired Specialization(s)</label>
<?= $form->field($spec, 'id')->label(false)->widget(Select2::classname(), [
'data' => $model->getAllSpecializations(),
'options' => ['placeholder' => 'You can choose more than one specialization ...'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true
],
]);
?>
</div>
Please, your reply will be appreciated. Thanks
I think you need to add the saved values as the initial data? Like so:
'value' => $savedDataArray, // initial value
http://demos.krajee.com/widget-details/select2#usage-tags
After much digging into the code, i found a way on how to display selected database values into a multi-select option using Yii Select2
My Model
public function getCandidateLanguage()
{
$langValues = (new \yii\db\Query())
->select('c.language_id AS id, l.lang_name')
->from('candidate_language c ')
->innerJoin('languages l','c.language_id = l.id')
->where('c.candidate_id='.$this->candidate_id)
->orderBy('c.language_id')
->all();
return \yii\helpers\ArrayHelper::map($langValues,'id','lang_name');
}
My View
use kartik\widgets\Select2;
<?php
//the line below is to fetch the array key of $model->getCandidateLanguage() array
$lang->id = array_keys($model->getCandidateLanguage()); // value to initialize
echo Select2::widget([
'model' => $lang,
'attribute' => 'id',
'data' => $model->getAllLanguages(),
'options' => ['placeholder' => 'Choose multiple languages'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'tags' => true,
],
]);
?>
Hope it help someone who is facing the same issue.

how to integrate multimodelform with echmultiselect yii?

I am running into a little snag with combining extensions "EchMultiSelect" and "MultiModelForm" of YII framework.
What I'm trying to do is copy a set of form elements one of which elements is an EchMultiSelect widget.
According to tutorial on the jqRelCopy page, I would need to pass a copy of the element (datePicker in their example) to the 'jsAfterNewId' option:
'jsAfterNewId' => JQRelcopy::afterNewIdDatePicker($datePickerConfig),
So, I tried to modify that to:
'jsAfterNewId' => MultiModelForm::afterNewIdMultiSelect($memberFormConfig['elements']),
Where I also added the following to MultiModelForm.php:
public static function afterNewIdMultiSelect($element)
{
$options = isset($element['options']) ? $element['options'] : array();
$jsOptions = CJavaScript::encode($options);
return "if(this.attr('multiple')=='multiple'){this.multiselect(jQuery.extend({$jsOptions}));};";
}
its copied and working properly when i am using Add Person link but what happens if i am adding/cloning three items for example and when i change the third item multiselct option then its reflecting to the first multiselect dropdown only this is same for other as well also when i am adding new items by clicking on the Add Person link then its cloning the same element to the new row item
here is the code for the form configuration variables and multimodel widget call.
//$userList=array of the userIds from users table
$memberFormConfig = array(
'elements'=>array(
'userId'=>array(
'type'=>'ext.EchMultiSelect.EchMultiSelect',
'model' => $User,
'dropDownAttribute' => 'userId',
'data' => $userList,
'dropDownHtmlOptions'=> array(
'style'=>'width:500px;',
),
),
...
...
));
calling the MultiModelForm widget from the same view file
$this->widget('ext.multimodelform.MultiModelForm',array(
'id' => 'id_member', //the unique widget id
'formConfig' => $memberFormConfig, //the form configuration array
'model' => $model, //instance of the form model
'tableView' => true,
'validatedItems' => $validatedMembers,
'data' => Person::model()->findAll('userId=:userId', array(':userId'=>$model->id)),
'addItemText' => 'Add Person',
'showAddItemOnError' => false, //not allow add items when in validation error mode (default = true)
'fieldsetWrapper' => array('tag' => 'div',
'htmlOptions' => array('class' => 'view','style'=>'position:relative;background:#EFEFEF;')
),
'removeLinkWrapper' => array('tag' => 'div',
'htmlOptions' => array('style'=>'position:absolute; top:1em; right:1em;')
),
'jsAfterNewId' => MultiModelForm::afterNewIdMultiSelect($memberFormConfig['elements']),
));
Can someone help me with this please?
Thanks in advance!
After a long searching and googleing i found the solution for this, just replace the function in your MultiModelForm.php:
public static function afterNewIdMultiSelect($element)
{
$options = isset($element['options']) ? $element['options'] : array();
$jsOptions = CJavaScript::encode($options);
return "if ( this.hasClass('test123456') )
{
var mmfComboBoxParent = this.parent();
// cloning autocomplete and select elements (without data and events)
var mmfComboBoxClone = this.clone();
var mmfComboSelectClone = this.prev().clone();
// removing old combobox
mmfComboBoxParent.empty();
// addind new cloden elements ()
mmfComboBoxParent.append(mmfComboSelectClone);
mmfComboBoxParent.append(mmfComboBoxClone);
// re-init autocomplete with default options
mmfComboBoxClone.multiselect(jQuery.extend({$jsOptions}));
}";
}
Thats It....!!
Thanks...!!!

Changing CForm dropdown name attribute in Yii

I'm using Yii and I'm having a little problem with some dropdowns.
Basically I'm using CForm to display some dropdown menus of courses. A student can select up to two courses and for each course choice the student can select a 1st choice and second. It is a requirement that each course choice is inserted separately into the database. For example, it a student wants to study 2 courses and wants to have a 1st and 2nd priority course, they would choose like this:
Course one - 1st Priority
Course one - 2nd Priority
Course two - 1st Priority
Course two - 2nd Priority
This would put 4 new rows into the database. The administrators of the courses want this displayed as 4 dropdown menus containing the courses.
At the moment, I'm testing with just the 1st and 2nd priorities of course one, but the problem is that course one - priority one is always empty unless a value is selected for priority two. Then priority one gets the same value as priority two, even though two different courses are selected. I've been following this tutorial Form Builder as I am using the Wizard Behavior which uses CForm to build the forms.
Here is my code so far, again only dealing with "course one":
This is a snippet of the relevant code from the controller:
// inside controller
$model = new CourseChoice();
$form = new CForm('application.views.wizard.ccForm', $model);
$form['courseOneP1']->model = new CourseChoice();
$form['courseOneP2']->model = new CourseChoice();
$c1p1 = $form['courseOneP1']->model;
$c1p2 = $form['courseOneP2']->model;
// Here I am just reading the attributes and exiting for testing
if ($form->submitted()&& $form->validate()) {
echo '<pre>';
print_r($c1p1->attributes);
print_r($c1p2->attributes);
echo '</pre>';
exit;
..........
And here is code in the form in ccForm
return array(
'showErrorSummary' => true,
'title' => 'Course Choice 1',
'elements' => array(
// Course 1 - 1st Priority
'courseOneP1' => array(
'type' => 'form',
'elements' => array(
'course' => array(
'label' => '1st Priority',
'type' => 'dropdownlist',
'id' => 'c1p1',
'prompt' => 'Select 1st Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
),
),
// Course 1 - 2nd Priority
'courseOneP2' => array(
'type' => 'form',
'elements' => array(
'course' => array(
'label' => '2nd Priority',
'type' => 'dropdownlist',
'id' => 'c1p2',
'prompt' => 'Select 2nd Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
),
),
),
'buttons' => array(
'previous' => array(
'type' => 'submit',
'label' => 'Previous'
),
'submit' => array(
'type' => 'submit',
'label' => 'Next'
)
)
);
So lets say I choose 2 courses, one with an id of 15 and the other with an id of 86, I get the following when I print_r() both dropdowns:
Array // Dropdown 1
(
[course] => 86
.... // other irrelevant attributes
)
Array // Dropdown 2
(
[course] => 86
.... // other irrelevant attributes
)
Update
I've been looking further into this and when I look at firebug, I see that both dropdowns have the same name:
<div class="row field_course">
<label for="c1p1">1st Priority</label>
<select id="c1p1" name="CourseChoice[course]">
</div>
<div class="row field_course">
<label for="c1p2">2nd Priority</label>
<select id="c1p2" name="CourseChoice[course]">
</div>
So the 2nd menu is overwriting the first. But how can I change this? If I change 'course'=>array(.... in the CForm for either subform, the applicable dropdown does not render. I have already tried adding 'name'=>'course1' in the form but it makes no difference.
Couldn't you just set the name of the 2nd priority input element?
'course' => array(
'label' => '2nd Priority',
'name' => 'course2',
'type' => 'dropdownlist',
'id' => 'c1p2',
'prompt' => 'Select 2nd Priority Course',
'items' => CHtml::listData(CoursePeriod::model()->with('course')->findAll("year = 2014"), 'id', 'course.course_name'),
)
Just to answer my own question and close it as it is pretty old now, CForm does not support tabular input and would need to extended to achieve this. Probably not a big job but in the end I convinced management that the four dropdowns design was horrible. :-) I went with a more flexible design showing a gridview of courses in a pop-up to choose courses instead which works well and is less confusing for the user.
Anyone interested in this issue can see the open issue here. There is a link in there to view a possible implementation of extending CForm, though this was posted at the end of 2009.

Yii: CGridView fails to render because missing ID field

In my model, the id field is missing and I need to render a gridview which failed to render:
CException
Property "DocumentRequests.id" undefined.
I have a field document_request_id instead of id
Does the GridView work with the id field and if so, how can I override it?
I found the answer, how to override the id field if missing:
$data_provider_grid = new CArrayDataProvider($data_provider_grid, array(
'id' => 'user',
'keyField' => 'document_request_id',
'pagination' => array(
'pageSize' => $pageSize,
),
));

yii tokeninput extension cursor focus on wrong place after same match?

I am having problem with yii tokeninput extension. When i search name it gives the user list and if i select any name and if that name is also selected previous than the cursor point after the selected item, it does not point at the end of all the the item in the input box.
I am using this configuration.
$this->widget('ext.tokeninput.TokenInput', array(
'model' => $model,
'attribute' => 'USER_ID',
'url'=>$this->createUrl('user/searchUserNames'),
'options' => array(
'allowCreation' => false,
'preventDuplicates' => true,
// 'resultsFormatter' => 'js:function(item){ return “<li><p>” + item.name + “</p></li>” }',
'theme' => 'facebook',
//'hintText' => 'Type',
'prePopulate' => $prePopulate,
'processPrePopulate' => $processPrePopulate,
)
));
I have also lookout at the examples but does not find the solution. can any one help me ?
Loopj: jquery token input demo
plz comment the line number 509 in **jquery.tokeninput.js**
input_token.insertAfter(found_existing_token);
that line insert cursor after that selected item so if you comment
this line cursor is at the end of all names