I got an issue. I have 3 select inputs, that i want to fill in with same options. First input gets options, but another two don't. I've tried everything. Last thing, that i tried was select 3 different queries and fill each one separately. Unfortunately, i get same issue.
Thanks for advices.
Controller
$dataPbxObj1 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbxObj2 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbxObj3 = Sipend::find()
->select('cc_sip_end.*')
->leftJoin('cc_reseller_to_pbx', '`cc_reseller_to_pbx`.`ID_PBX` = `cc_sip_end`.`id`')
->where(["in", "cc_reseller_to_pbx.id_cc_reseller", $reseller->id_cc_reseller])->all();
$dataPbx1 = ArrayHelper::map($dataPbxObj1,'id','popis');
$dataPbx2 = ArrayHelper::map($dataPbxObj2,'id','popis');
$dataPbx3 = ArrayHelper::map($dataPbxObj3,'id','popis');
View (all this selects are same)
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(),
["data" => $dataPbx3,'hideSearch' => true]) ?>
You probably needs to use unique IDs - by default Yii generates ID based on field name, but with 3 identical fields, IDs will be the same, and Select2 init will apply only for first of them.
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX1'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX2'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
<?=$form->field($modelSip, 'ID_PBX')->widget(Select2::className(), [
'options' => ['id' => 'ID_PBX3'],
'data' => $dataPbx,
'hideSearch' => true,
]) ?>
BTW: you don't need to query options list 3 times, you can do it once and use the same result in 3 fields.
I am trying to implement the 'InArray' validator in Zend 2 on a form and it keeps on returning invalid. Here is the code:
The Form Element setup:
$enquiryType = new Element\Select('enquiryType');
$enquiryType->setLabel('* What is your enquiry about?')
->setLabelAttributes(array('class' => 'sq-form-question-title'))
->setAttribute('class', 'sq-form-field required')
->setValueOptions($enquiryTypes);
The Filter/Validator setup:
$enquiryType = new Input('enquiryType');
$enquiryType->getValidatorChain()
->addByName('InArray', array('haystack' => $enquiryTypes));
And here is the array that gets passed into $enquiryTypes via the module.config.php file
'enquiryCategories' => array(
'empty_option' => 'Please select an option',
array(
'label' => 'General enquiries',
'options' => array(
'skype' => 'Skype with your library feedback',
'question' => 'Ask a question',
'feedback' => 'Library feedback',
'eresource' => 'Electronic resources (e.g. e-book, e-journal, database)',
'webbridge' => 'WebBridge Problems',
'dro' => 'DRO'
)
),
array(
'label' => 'Application to review',
'options' => array(
'10400' => 'I\'ve returned the item',
'10401' => 'I didn\'t know about overdue points but now I have some',
'10402' => 'Why did I get this invoice?',
'10403' => 'The item I borrowed is lost',
'10404' => 'The item I borrowed has been damaged',
'10405' => 'I never got/had the book',
'10406' => 'Other'
)
)
),
I have tried different variations of this (using the recursive validation also) but have not been able to work this out.
One thing that I'd try, is as follows:
$enquiryType = new Input('enquiryType');
$enquiryType->getValidatorChain()
->addByName('InArray', array('haystack' => $enquiryTypes['enquiryCategories']));
The reason I say that, is that it looks like you might be creating an array inside of the array perhaps. Unless I've misunderstood the description.
If that isn't working for you then maybe you might need to explore the Explode option as pointed out in the following question
ZF2: How do I use InArray validator to validate Multiselect form element?
Good luck.
I finally got a working solution for this. I feel there should be a better solution but I could not find one.
Here is the filter I used:
$enquiryType = new Input('enquiryType');
$enquiryType->getValidatorChain()
->addByName('InArray', array('haystack' => array_keys(
$enquiryTypes[0]['options']+$enquiryTypes[1]['options']
)));
$enquiryType->getFilterChain()
->attach(new Filter\StringToLower())
->attachByName('StripTags');
Basically I had to disect the array options into a straight associative array. As the array remains static in this instance then this works well.
If the data becomes dynamic then something more would be required (loop through and find the 'option' key, add children to filter array etc...)
I am trying to use TbEditableColumn / X-Editable to make inline edits in the CGridView. I'm trying to use the usual $data variable to get row id by the intuitive "$data->id" to pass the line id in the dynamically generated url so that in the update action of the controller, I'd know which record to update.
However $data->id is just not working while creating URL. Interestingly the same $data variable is accessible while applying "Editable" property on the item. See below.
I don't want to go for JS:function way to fetch id of an element and pass it as argument in ajax call. This just doesn't seem elegant.
array(
'class' => 'editable.EditableColumn',
'name' => 'mrp',
'headerHtmlOptions' => array('style' => 'width:200px'),
'editable' => array(
'type' => 'text',
'apply' => '$data->id>10605',
'url' => '/product/changeMRP/id/$data->id',
),
),
In above code, this line works
'apply' => '$data->id>10605'
But this line doesn't
'url' => '/product/changeMRP/id/$data->id',
Why is it that? And how can i solve my problem?
I followed this question :
How can update db values using x-editable EditableColumn? but it didnt help
You can use createUrl() for creating URL and pass the id in array. Like this
'url' => "Yii::app()->createUrl('product/changeMRP', array('id'=>'$data->id'))",
Hope it will solve your problem.
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
);
I have a gridview which contains a checkbox column and also uses pagination. When I check some checkboxes in the first page and navigate to the second page and check another one in the second page, the options I checked in the first page is not retained there. Is it posssible to retain the checkbox values during pagination?
Code for Gridview is
$widget = $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model->search(),
'cssFile' => Yii::app()->baseUrl . '/media/js/admin/css/admingridview.css',
//'filter' => $model,
'ajaxUpdate' => true,
'enablePagination' => true,
'columns' => array(
array(
'name' => 'id',
'header' => '#',
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => '2',
'header' => 'Selected',
),
array(
'name' => 'fb_user_id',
'header' => 'FaceBook Id',
'value' => 'CHtml::encode($data->fb_user_id)',
),
array(
'name' => 'first_name',
'header' => 'Name',
'value' => 'CHtml::encode($data->first_name)',
),
array(
'name' => 'email_id',
'header' => 'Email',
'value' => 'CHtml::encode($data->email_id)',
),
array(
'name' => 'demo',
'type' => 'raw',
'header' => "Select",
'value' => 'CHtml::checkBox("email[]","",array("class"=>"check","value"=>$data->email_id))',
),
),
));
Edit:
Extension for remembering the selected options in gridview,check this link Selgridview
Thanks to bool.dev
You could use sessions/cookies to store the checked values. I'm not very sure how to make cookies work, so i'll tell you how to do it with sessions. Specifically the user session that yii creates.
Now to use sessions we need to pass the checked (and unchecked) ids to the controller, therefore we'll modify the data being sent to the controller on every ajax update(i.e between paginations), to do this we exploit the beforeAjaxUpdate option of CGridView.
I'm also using CCheckBoxColumn instead of the following in your code(of course you can modify the solution to suit your own needs):
array(
'name' => 'demo',
'type'=>'raw',
'header' => "Select",
'value' => 'CHtml::checkBox("email[]","",array("class"=>"check","value"=>$data->email_id))',
),
GridView Changes:
<?php $this->widget('zii.widgets.grid.CGridView', array(
// added id of grid-view for use with $.fn.yiiGridView.getChecked(containerID,columnID)
'id'=>'first-grid',
'dataProvider'=>$model->search(),
'cssFile' => Yii::app()->baseUrl . '/media/js/admin/css/admingridview.css',
// added this piece of code
'beforeAjaxUpdate'=>'function(id,options){options.data={checkedIds:$.fn.yiiGridView.getChecked("first-grid","someChecks").toString(),
uncheckedIds:getUncheckeds()};
return true;}',
'ajaxUpdate'=>true,
'enablePagination' => true,
'columns' => array(
array(
'name' => 'id',
'header' => '#',
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
array(
'name' => 'fb_user_id',
'header' => 'FaceBook Id',
'value' => 'CHtml::encode($data->fb_user_id)',
),
array(
'name' => 'first_name',
'header' => 'Name',
'value' => 'CHtml::encode($data->first_name)',
),
array(
'name' => 'email_id',
'header' => 'Email',
'value' => 'CHtml::encode($data->email_id)',
),
/* replaced the following with CCheckBoxColumn
array(
'name' => 'demo',
'type'=>'raw',
'header' => "Select",
'value' =>'CHtml::checkBox("email[]","",array("class"=>"check","value"=>$data->email_id))',
),
*/
array(
'class' => 'CCheckBoxColumn',
'selectableRows' => '2',
'header'=>'Selected',
'id'=>'someChecks', // need this id for use with $.fn.yiiGridView.getChecked(containerID,columnID)
'checked'=>'Yii::app()->user->getState($data->email_id)', // we are using the user session variable to store the checked row values, also considering here that email_ids are unique for your app, it would be best to use any field that is unique in the table
),
),
));
?>
Pay special attention to the code for beforeAjaxUpdate and CCheckBoxColumn, in beforeAjaxUpdate we are passing checkedIds as a csv string of all the ids(in this case email_ids) that have been checked and uncheckedIds as a csv string of all the unchecked ids, we get the unchecked boxes by calling a function getUncheckeds(), which follows shortly. Please take note here, that when i was testing i had used an integer id field (of my table) as the unique field, and not an email field.
The getUncheckeds() function can be registered like this anywhere in the view file for gridview:
Yii::app()->clientScript->registerScript('getUnchecked', "
function getUncheckeds(){
var unch = [];
/*corrected typo: $('[name^=someChec]') => $('[name^=someChecks]') */
$('[name^=someChecks]').not(':checked,[name$=all]').each(function(){unch.push($(this).val());});
return unch.toString();
}
"
);
In the above function pay attention to the selectors and each and push function.
With that done, we need to modify the controller/action for this view.
public function actionShowGrid(){
// some code already existing
// additional code follows
if(isset($_GET['checkedIds'])){
$chkArray=explode(",", $_GET['checkedIds']);
foreach ($chkArray as $arow){
Yii::app()->user->setState($arow,1);
}
}
if(isset($_GET['uncheckedIds'])){
$unchkArray=explode(",", $_GET['uncheckedIds']);
foreach ($unchkArray as $arownon){
Yii::app()->user->setState($arownon,0);
}
}
// rest of the code namely render()
}
That's it, it should work now.
For developing that scheme you would need to know working of what happens when you navigate.
When ever you navigate to a pagination page ajax calls are made and new data is received and it is fetched from CActive Record or what ever the data source. New data is in accordance of database records or source records. when you come back to previous page again Ajax call is made and content is updated so same comes as it is in database.
what i feel is you should save data of checked items temporary and make it permanent if action is made.
You can do something like this
<script type="text/javascript">
$("input:checkbox").click(function () {
var thisCheck = $(this);
if (thisCheck.is (':checked')){
// do what you want here, the way to access the text is using the
// $(this) selector. The following code would output pop up message with
// the selected checkbox text
$(this).val());
}
});
</script>
you can save temporary storage somewhere
Also make this work on normal form submit:
I wanted to add this as a comment on bool.dev's answer, but I do not have enough reputation to do that yet. So I had to put it in a separate answer.
bool.dev, your answer is great and it works well, thanx.
However, as intended, it only works when ajax calls update the gridview. I have the gridview forming part of a form, so I wanted it to also work on normal submission of the form, otherwise the checkboxes are not loaded again when there are other validation errors on the form.
So, in ADDITION to what you did, I added hidden fields on my form e.g.:
<input type="hidden" name='checkedBox1' id='checkedBox1' value=''>
<input type="hidden" name='uncheckedBox1' id='uncheckedBox1' value=''>
Then, before submitting the form, my sumbit button runs your getChecked() and getUncheckeds() functions and store their results in the hidden fields:
if ($('#checkedBox1').length >0) {$('[name=checkedBox1]').val(getChecked());}
if ($('#uncheckedBox1').length >0) {$('[name=uncheckedBox1]').val(getUncheckeds());}
In the controller, besides from checking for $_GET['checkedIds'], you also check for $_POST['checkedBox1'] and store its values to session in the same way you do for $_GET['checkedIds'], using the same session variable.
Do the same with $_POST['uncheckedBox1'].
That should work.