Yii and YiiBooster widget modal confirm to url - yii

Using the yiibooster modal widget, I want it to go to a different url if user clicks ok to confirm.
$this->widget('bootstrap.widgets.TbButton', array(
'label'=>'Restart Game',
'type'=>'warning',
'htmlOptions'=>array(
'onclick'=>'js:bootbox.confirm("Are you sure?",
function(confirmed){console.log("Confirmed: "+confirmed);})'
),
));
//this is the url i want it to go to when ok confirmed is clicking
'url' =>array('site/restart?gameId='.$gameRecord->id)

Try this code :
$url = Yii::app()->createAbsoluteUrl('site/restart', array('id'=>$gameRecord->id));
$this->widget('bootstrap.widgets.TbButton', array(
'label'=>'Restart Game',
'type'=>'warning',
'htmlOptions'=>array(
'onclick'=>'js:bootbox.confirm("Are you sure?",
function(confirmed){
if(confirmed) {
window.location = "'.$url.'";
}
})'
),
));

Should it be this:
'url'=>array('site/restart', array('id'=>$gameRecord->id))
Assuiming you have or need the rules in the config?

Related

Errors on submit in a popup box?

How to show the validation errors in a form in a pop dialogue box?Instead of showing it in the top of the form as a separate div, i want to show those errors in a popup dialogue box so that user clicks okay and dismiss the box.How to do this in yii?
Register your own javascript function name to the afterValidate, which is one of the options in clientOptions property in CActiveForm form class.
Your form declaration should have
'clientOptions' => array(
'validateOnSubmit' => true,
'afterValidate' => 'js:myFunc',
),
And Your form will appear like bellow
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'a-form',
'enableClientValidation' => true,
'enableAjaxValidation' => true,
'errorMessageCssClass' => 'required',
'clientOptions' => array(
'validateOnSubmit' => true,
'afterValidate' => 'js:myFunc',
),
));
?>
------Your form fields------------
------Your form fields------------
------Your form fields------------
<?php $this->endWidget(); ?>
Now, Your myFunc code:
<script type="text/javascript" charset="utf-8">
function myFunc(form, data, hasError)
{
if (hasError)
{
var errors='';
$.each(data, function(obj)
{
errors+=data[obj][0]+"\n";
});
alert(errors);
// Do what ever you want
return true;
}
}
</script>
If you enable client side validation, then you will get error message under textbox, dropdown. There is no in-built option for poping up error message.
still if you need popup error message display, then you have to do with jquery. Then add in Yii forum to help others as well :-)
Refer this link (Yii forum) for details about client side validation
You can build the HTML view yourself with a custom CFormModel and use getError() method in a modal popup.
See : http://www.yiiframework.com/doc/api/1.1/CModel#getError-detail
and : http://www.yiiframework.com/doc/api/1.1/CFormModel

yii adding font awesome icons in bootstrap nav bar

I'm trying to add an icon to my bootstrap navbar
this is the icon i'm trying to add <i class="icon-fa-comments icon-3x "></i>
i'm currently using this font awesome extension and this bootstrap extension
the icon shows when it's in my html, but not when i add it to nav bar. any idea what i'm missing? sorry still new to Yii, can't find a key other than label to do what i want. tried htmlOptions but no luck too
<?php
$this->widget('bootstrap.widgets.TbNavbar',array(
'items'=>array(
//'brand' => CHtml::encode(Yii::app()->name), //site name
//'fixed' => true,
array(
'class'=>'bootstrap.widgets.TbMenu',
'items'=>array(
array('label'=>'Home', 'url'=>array('/')),
array('label'=>'About', 'url'=>array('/about')),
array('label'=>'Contact', 'url'=>array('/contact')),
array('url'=>Yii::app()->getModule('user')->loginUrl, 'label'=>Yii::app()->getModule('user')->t("Login"), 'visible'=>Yii::app()->user->isGuest),
array('url'=>Yii::app()->getModule('user')->registrationUrl, 'label'=>Yii::app()->getModule('user')->t("Register"), 'visible'=>Yii::app()->user->isGuest),
array('url'=>Yii::app()->getModule('user')->profileUrl, 'label'=>Yii::app()->getModule('user')->t("Profile"), 'visible'=>!Yii::app()->user->isGuest),
array('url'=>Yii::app()->getModule('user')->logoutUrl, 'label'=>Yii::app()->getModule('user')->t("Logout").' ('.Yii::app()->user->name.')', 'visible'=>!Yii::app()->user->isGuest),
),
),
array(
'class'=>'bootstrap.widgets.TbMenu',
'htmlOptions'=>array('class'=>'pull-right'),
'items'=>array(
array('label'=>'<i class="icon-fa-comments icon-3x "></i>', 'url'=>'#'),
array('label'=>'Dropdown', 'url'=>'#', 'items'=>array(
array('label'=>'Action', 'url'=>'#'),
array('label'=>'Another action', 'url'=>'#'),
array('label'=>'Something else here', 'url'=>'#'),
'---',
array('label'=>'Separated link', 'url'=>'#'),
)),
),
),
),
)); ?>
In order to display icons in Yii-2 components like Navbar or Nav, you must set encodeLabels to false.
http://www.yiiframework.com/extension/yii2-icons/
try this
array('icon'=>'icon-fa-comments icon-3x', 'url'=>'#'),
Some times you get squares on the web page although you have added the css file font-awesome.css successfully. The solution is very easy you and need to place the font directory (containing four font files) as well inside the CSS folder where you have placed font-awesome.css.
The font directory contains four other font files.
fontawesome-webfont.eot
fontawesome-webfont.ttf
fontawesome-webfont.woff
FontAwesome.otf
Jay is right but I found the explanation hard to follow, so I am adding the code here. This is what the working solution looks like:
In the php header (presuming you have installed kartik icons)
use kartik\icons\Icon;
Icon::map($this);
...
In the nav widget:
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'encodeLabels' => false, // declaration goes here (before the items)
'items' => [
[
'label' => Icon::show('home',[
'class'=>'your-custom-class-if-needed'
]),
'url' => ['/survey-visits/index'],
'visible' => !Yii::$app->user->isGuest,
],
You can label encoding for individual element like below:
array('label'=>'<i class="icon-fa-comments icon-3x "></i>', 'url'=>'#', 'encode'=>false)

How to change default login action

I am newbie for Yii. I have created my separate login page in my controller. Here my question is "When
When session expires it will ask for re-login. and it will transfer to site/login page.
Instead of site/login I want to redirect it to xyz/login page. Where I have to change this?
change in the main.php
array(
// ......
'components'=>array(
'user'=>array(
'loginUrl'=>array('xyz/login'),
),
),
)
in your config/main.php,
'urlManager' => array(
...
'rules' => array(
'xyz/login' => 'site/login', // Add this
...

Yii - Ajax Get based on drop down selection

I am trying to display the price based on the selection of product from a drop down.
View contains:
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'plan-form',
'enableAjaxValidation'=>true,
)); ?>
<?php echo $form->dropDownListRow($model, 'plan_id',
$planList, array('id'=>'planid', 'prompt'=>'Select Plan',
'ajax' => array('type'=>'GET',
'url'=> Yii::app()->createUrl('mbr/plan/ajaxGetPrice'),
'update'=>'#price'))); ?>
<div id="price">0.00</div>
<?php $this->endWidget(); ?>
Action:
public function actionAjaxGetPrice()
{
Yii::log("Within AjaxGetPrice");
if (Yii::app()->request->isAjaxRequest)
Yii::log("ajax request");
else
Yii::log("regular request");
//$plan=Plan::model()->findByPk((int) $_GET['plan_id']);
//Yii::log(serializ($plan));
// echo $plan->price;
echo "10";
Yii::app()->end();
}
It is not updating the price. Its not calling the action at all. I looked at the suggestions found in Yiiframework and here and tried those and still no luck.
when I add this to the view
<?php
echo CHtml::ajaxLink(
"Get Price",
Yii::app()->createUrl('mbr/plan/ajaxgetprice'),
array( // ajaxOptions
'type' => 'GET',
'update' => '#price'),
array( //htmlOptions
'href' => Yii::app()->createUrl('mbr/plan/ajaxgetprice')
)
);
?>
I get the response when I click on the link, but it renders a separate page with value "10". I have URLFormat = Path.
What am I doing wrong? Any pointers?
You can also try this as shown
<?php echo CHtml::dropDownList('categories','',
$category,
array('ajax'=>array('type'=>'POST','url'=>CController::createUrl('yourController/GetId'),'update' =>'#data'))
);?>
yourController action
public function actionGetId(){
echo 10;
}
And the div
<div id="data">0.00</div>
I'm not really sure how to do this the Yii way but the following javascript should be useful.
$("#yourdropdownidhere").change(function(){
$.ajax({
url: 'your url here', //(dynamically generated by product item)
type: "get",
success: ajaxSuccessHandler
});
});
function ajaxSuccessHandler(obj) {
$('#price').html(obj);
}
I also suggest sending a JSON object as the response for AJAX requests, so it will have an actual structure. A great blog post on this to check out would be Fully ajax website with Yii
I had the same problem and solved it on the controller that renders the view. If you are using renderPartial assure that the processOutput() option is set to TRUE, otherwise Yii will not attach any Javascript.
If you use render instead of renderPartial it also works.

Yii : CJuidialog and iframe

I'm using Yii framework. I need to create dialog using iframe. How can i create CJuidialog in iframe? If you have some experience with it, please share.
Thanks.
I am using CjuiDialog For creating playlist
In My Layout (playlist Button)
`Add To playList`
By Clicking Above Button id refer the following code
In View Page
$('#addavtoplaylist').live('click',function(){
$('#cru-frame').attr('src','".Yii::app()->createAbsoluteUrl("playlist/add",array('id'=>$this->model->av_id))."');
$('#cru-dialog').dialog('open');
return false;
});
add the dialog code in the same page
<?php
//--------------------- begin new code --------------------------
// add the (closed) dialog for the iframe
$this->beginWidget('zii.widgets.jui.CJuiDialog', array(
'id'=>'cru-dialog',
'options'=>array(
'title'=>'Add To Play List',
'autoOpen'=>false,
'modal'=>true,
'width'=>550,
'height'=>300,
'close'=>'js:function(){
}',
),
));
?>
<iframe id="cru-frame" width="100%" height="100%"></iframe>
<?php
$this->endWidget();
//--------------------- end new code --------------------------
?>
Use the following code in your view file
$this->beginWidget('zii.widgets.jui.CJuiDialog',array(
'id'=>'mydialog',
// additional javascript options for the dialog plugin
'options'=>array(
'title'=>'Dialog box 1',
'autoOpen'=>false,
),
));
echo 'dialog content here';
$this->endWidget('zii.widgets.jui.CJuiDialog');
// the link that may open the dialog
echo CHtml::link('open dialog', '#', array(
'onclick'=>'$("#mydialog").dialog("open"); return false;',
));