Yii - disabling drop down when another is selected - yii

So I have 2 $form->dropDownList
How can I make it in a way when if either one of the dropdown is selected, the other one gets set to value null and disabled, vice versa.
What options can I add into the array() so it behaves the way I want?
Please advise. Thanks in advance.

You need to handle it via javascript or jquery. First you should define an ID for two dropdowns in this way:
<?php echo $form->dropDownList($yourModel, 'attribute', CHtml::listData(...), array("id" => 'dropDown1')); ?>
<?php echo $form->dropDownList($yourModel, 'attribute', CHtml::listData(...), array("id" => 'dropDown2')); ?>
And in the script:
$(document).ready(function(){
if($("#dropDown1").val())
$("#dropDown2").attr("disabled", "disabled");
else
$("#dropDown2").removeAttr("disabled");
if($("#dropDown2").val())
$("#dropDown1").attr("disabled", "disabled");
else
$("#dropDown1").removeAttr("disabled");
$($("#dropDown1").on("change", function(){
if($(this).val())
$("#dropDown2").attr("disabled", "disabled");
else
$("#dropDown2").removeAttr("disabled");
});
$($("#dropDown2").on("change", function(){
if($(this).val())
$("#dropDown1").attr("disabled", "disabled");
else
$("#dropDown1").removeAttr("disabled");
});
});

Related

How can I change the checkboxlist to look similar to on/off switch?

Is there any way to change the checkboxlist widget to look similar to the on/off switch presented in Kartik's Switch Input Widget, but in a group?
I'm using this code:
echo $form->field($model, 'blocked_list')->checkboxList($array_list);
Which is very simple to use, but produces "simple" list...
I've tried with Karitk's like this:
foreach ($array_list as $category_id=>$category_name) {
echo '<label class="control-label">' . $category_name . '</label>';
echo SwitchInput::widget([
'name'=>'blocked_list',
'value'=>in_array($category_id, $model->blocked_list),
'pluginOptions' => [
'size' => 'mini',
],
]);
But it does not link with the model, and the Form looses it format
Any ideas, please?
I've got it!
foreach ($category_array as $category_id=>$category_name) {
if ( isset($model->blocked_list[$category_id]) )
$model->blocked_list[$category_id] = true;
else
$model->blocked_list[$category_id] = false;
echo $form->field($model, 'blocked_list[' . $category_id . ']')
->label($category_name)
->widget(SwitchInput::classname(), []);
}
I just added a validate "if" to ask if the variable to display exists, and then set the correct value for the model
Hope it helps someone

Cake PHP Update single fields in View - Undefined Variable

i search a long time, but i dont find a solution for my problem.
The case is, that i have a few buttons integrated in my view and clicking on them affected updating single input fields. The problem is that i have warnings, that variables are undefined in view. I understand why and how i suppress them, but i`m not sure, if this is a good solution. Is there a better way to solve this? What is best practice?
Here is my code from the view file:
<?php
echo $this->Form->create('Excel', array('type' => 'file'));
echo $this->Form->file('File');
echo $this->Form->submit('Upload xls File');
echo $this->Form->end();
echo $this->Form->create('Config');
//echo $this->Form->input('Name');
echo $this->Form->input('vlanNumber');
echo $this->Form->input('description', array('value' => $description));
echo $this->Form->input('preSharedKey', array('value' => $preSharedKey));
echo $this->Form->button('generate', array('name'=>'generateButton'));
echo $this->Form->input('customerPeerIp', array('default' => 'id_of_default_val','value' => $cusPeerIp));
The generate button affect a new preSharedKey. And the upload of the csv affected an update of the other fields.
The relevant code of the controller is this:
public function inputData() {
if ($this->request->is('post')) {
$post_data = $this->request->data;
if (isset($this->request->data['show'])) { //Submit Button was clicked
$this->Session->write('Configuration',$post_data); //Store the input fields in the session
return $this->redirect(array('action' => 'showPreview'));
} else if (isset($this->request->data['cancel'])) { // Cancel button was clicked: Go back to index site
return $this->redirect(array('action' => 'index'));
} else if (isset($this->request->data['generateButton'])) {
return $this->set('preSharedKey', $this->getRandomString(20)); //Set a Pre Shared Key with 30 signs
}
if (!empty($this->data) && is_uploaded_file($this->data['Excel']['File']['tmp_name'])) {
$this->importData($this->data['Excel']['File']['tmp_name']);
$excel=new Excel();
$values=$excel->getParams($this->data['Excel']['File']['tmp_name']);
$this->set('description',$values['description']);
$this->set('cusPeerIp',$values['cust_peer']);
return;
//this calls the Excel Class function
}
//print_r($post_data);
//echo $post_data['Config']['Name'];
//echo $this->request['Config']['task_1'];
}
$this->set('description','');
$this->set('cusPeerIp','');
$this->set('preSharedKey', '');
}
Can you please help me?

DropDownList won't POST value by using ajax

I created DropDownList and I'm trying to fill div with content and I know how to do it. But problem is that my dropdown list won't POST value.
This is code from view\index.php
<?php
echo CHtml::dropDownList('parovi', '', $model->dropDownListParovi(), array
(
'class'=>'dropDownListLegloIzaberiPar',
'empty'=>array('-'=>Yii::t('default', 'PAR_UZGOJNI_DNEVNIK_LEZENJA_IZABERI_PAR')),
'ajax'=>array
(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/paruzgojni/ajaxIzlistajLegla'),
'update'=>'#legla',
),
)
);
?>
<div id="legla"></div>
And this is from controller
public function actionAjaxIzlistajLegla()
{
echo $_POST['parovi'];
}
this AjaxIzlistaijLegla action is inside accessRules(). And it's working cause When I replace echo $_POST['parovi']; with echo "Hello"; it works, it updates my div. But I don't know why it won't POST that value from drop down list.
jquery is included in head.
Answer is I should wrap my dropDownList with CActiveForm like this
$this->beginWidget('CActiveForm', array(
'id'=>'par-uzgojni-ispis-legla-form',
));
echo CHtml::dropDownList('parovi', '', $model->dropDownListParovi(), array
(
'class'=>'dropDownListLegloIzaberiPar',
'empty'=>array('-'=>Yii::t('default', 'PAR_UZGOJNI_DNEVNIK_LEZENJA_IZABERI_PAR')),
'ajax'=>array
(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/paruzgojni/ajaxIzlistajLegla'),
'update'=>'#legla',
),
)
);
$this->endWidget();
Because I didn't wrap it, it couldn't POST any data to my controller

Check if user are friend to display data in members-loop buddyprress

Can someone please help me out, I have a profile field named BB Pin and i use
BB Pin:<?php bp_member_profile_data('field=BB Pin');?>
to show the data in members-loop.php and it work fine but i want to do it so that if you are not friend the field value is *****.
Something like
if is friend{
BB Pin:<?php bp_member_profile_data('field=BB Pin');?>
}
else{
****.
Thanks for you help.
Regards
Update:
I have just tried the below code in the members-loop.php but did not work.
<?php
global $bp; $friend = BP_Friends_Friendship::check_is_friend( $bp->loggedin_user->id, $bp->displayed_user->id ); if ( $bp->loggedin_user->id || $friend == 'is_friend') : ?> echo <?php bp_member_profile_data('field=BB Pin');?> <?php else : ?> echo *************** <?php endif; ?>
Check if logged in user is friend or not please use below code:
global $bp;
$is_friend = friends_check_friendship( $bp->loggedin_user->id, $bp->displayed_user->id );
if($is_friend) {
BB Pin:<?php bp_member_profile_data('field=BB Pin');?>
} else{
BB Pin: ***;
}
$is_friend = friends_check_friendship( bp_loggedin_user_id(), bp_displayed_user_id() );
if($is_friend) {
BB Pin:<?php bp_member_profile_data('field=BB Pin');?>
} else{
BB Pin: ***;
This one should work in the loop! You canĀ“t use the global Variable $bp in the loop, but there are other variables for that particular task. In this case I just changed
$bp->loggedin_user->id, $bp->displayed_user->id
to the ones in my code example!

Best way to display current logged-on user in default.ctp?

I am in the process of customizing the default.ctp file and I am trying to display the currently logged on user's name on the top of the page.
In app_controller.php, I have the following:
function beforeFilter()
{
$user = $this->Auth->user();
if($user != null)
{
$this->Session->write('user_name',$user['User']['username']);
}
}
And in default.ctp, I have:
$user = $this->Session->read('Auth.User');
if(!empty($user))
{
echo 'Hello, ' . $user['user_name'];
}
However, it seems like the value $user_name is not getting set anywhere.
What am I doing wrong? Is there a better way to accomplish this?
Update: I've modified it as described in the answer, but it still doesn't work. I get an error:
Undefined index: user_name [APP/views/layouts/default.ctp, line 21]
you can also use the SessionHelper directly in the view / layout
$user = $this->Session->read('Auth.User');
if(!empty($user)) {
echo 'Hi ', $user['user_name'];
}
Cakephp 2.x:
<?php if (AuthComponent::user('id')): ?>
<p class="navbar-text pull-right">
Logged in as <?= AuthComponent::user('name') ?>
</p>
<?php endif; ?>
$user = $this->Session->read('Auth.User');
if(count($user))
echo $user['name'];