Yii CLinkPager & urlManager rules - yii

I have the following issue:
When I press a button in my pagination, CLinkPager generates the link as follows:
page/videos/445/?page=2
What I need is something like this:
page/videos/445/page/2
Furthermore, I need something like:
name/videos/2
where name is url of user_id = 445.
I've set some rules in urlManager but they don't quite do the job:
For step 1:
'<controller:\w+>/<action:\w+>/<id:\d+>/<page:\d+>' => '<controller>/<action>',
Any help is appreciated. Thank you in advance!

Here example for page/videos/445/page/2.This will help you understand:
//config
'rules'=>array(
'page/videos/<number:\d+>/page/<pageId:\d+>' => '/site/test',
...
//...SiteController...
//url for call www.your_site.lh/page/videos/445/page/2
public function actionTest($number, $pageId)
{
echo '<pre>';
print_r($_GET);
echo "$number<br/>";
echo "$pageId<br/>";
echo '</pre>';
die();
}
URL Management
If you want to change generation links in pager you can override CLinkPager.

Related

How do I pass variable from action to another action of a controller in yii?

Here is my action
public function actionUpload()
{
$model=new Sop;
if(isset($_FILES['files'])){
print_r($_POST['name']);exit;
$tmp_name = $_FILES['files']['tmp_name'][0];
$filename = $_FILES['files']['name'][0];
$new_url = Yii::app()->basePath."/docfile/".$filename;
move_uploaded_file($tmp_name, $new_url);
/*if(move_uploaded_file($tmp_name, $new_url)){
//print_r($filename);exit;
echo $filename;
}*/
}
else "error";
}
Now i want to pass $tmp_name , $ filename and $new_url to another action in this controller.
I'm not exactly sure what you are trying to achieve as actions within controllers are designed to be processing requests and generating responses. It seems you are not using the Controller correctly.
If you however insist on using the controller-action you can use the redirect with a parameters as shown in the Documentation here: http://www.yiiframework.com/doc-2.0/yii-web-controller.html#redirect()-detail
An example could be (from inside a controller):
// /index.php?r=site/destination&tmp_name=$tmp_name&new_url=$new_url&filename=$filename
$this->redirect([
'site/destination',
'tmp_name' => $tmp_name,
'new_url' => $new_url,
'filename' => $filename,
]);
Please note that this is far from ideal. I'm not sure what you are trying to achieve but I would suggest using a Component for finishing the upload(?) job instead of sending parameters to a controller-action that finishes it.
You can use $this->forward('action') to execute another action without redirecting to that url:
$params = array(
'tmp_name' => $tmp_name,
'filename' => $filename,
'new_url' => $new_url,
);
$action = $this->createUrl('controller/action', $params);
$this->forward($action);
The other action header could look like this:
public function actionAction($tmp_name, $filename, $new_url)
lets say you want to pass $tmp_name , $filename and $new_url parameters to an action named "next" in same controller, it can be done simply like this,
in your actionUpload() call action next as $this->actionnext($tmp_name , $filename and $new_url).
public function actionUpload()
{
//code for actionupload()
$this->actionnext($tmp_name , $filename , $new_url); //call to next action.
}
in controller, definition of actionnext() should look like this:
public function actionnext($tmp_name , $filename , $new_url)
{
//your code goes here.
}
and in actionnext function defination "$tmp_name , $ filename , $new_url" are just variables so you can use any name here.

ClientValidation In Form not showing When I Send Id Variable To Jquery in YII

I Need Help in Yii, i get problem in my form, the problem is if i put id in textfield then client validation not showing for that field but another field no problem. This is my code in form
<div class="row">
<?php echo $form->labelEx($model,'latar_belakang'); ?>
<?php echo $form->textField($model,'latar_belakang',array('id' => 'latarbelakang','class' => 'input-block-level')); ?>
<?php echo $form->error($model,'latar_belakang'); ?>
<?php echo $form->labelEx($model,'rumusan_masalah'); ?>
<?php echo $form->textField($model,'rumusan_masalah',array('id' => 'rumusanmasalah','class' => 'input-block-level')); ?>
<?php echo $form->error($model,'rumusan_masalah'); ?>
</div>
for your knows i use id in textfield for calculaten field with jquery which i put in form . this is my jquery code in form
Yii::app()->clientScript->registerCoreScript('jquery');
Yii::app()->clientScript->registerScript('totalValues',"
function totalValues()
{
var lb = $('#latarbelakang').val() ;
var rm = $('#rumusanmasalah').val();
var tot = parseInt(lb) + parseInt(rm);
$('#nilaiakhir').val( tot );
}
", CClientScript::POS_HEAD);
Yii::app()->clientScript->registerScript('changeTotals', "
$('#latarbelakang').change( function(){ totalValues(); } );
$('#rumusanmasalah').change( function(){ totalValues();} );
", CClientScript::POS_END);
What i must do ? for solving this problem ?.
Thanks B4..
By default Yii generates ids of fields dynamically using model name and field name.
For example for 'password' field of 'User' model the id would be 'User_password'
You can inspect a field(to which you have not given your custom id) with firebug and see the auto generated id. also yii maps error messages with those auto generated ids in response. that is why your error messages are not showing.
what you can do is to try to call that js function with yii generated id.

How to write a URL rule in Yii framework?

<?php echo CHtml::link($value->title, array(Yii::app()->createUrl('forum/thread', array('id'=>$value->thread_id)))); ?>
i got a link
forum/thread/2
in my urlManager rules 'thread/<id:\d+>' => 'forum/thread',
how to change the rule and method createUrl?
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))
to get in url
forum/any-value/thread/2 or forum/php-for-newbies/thread/2
I am sorry for my english, thanks a lot
URL Manager rule should look like this:
'forum/<title:\w+>/thread/<id:\d+>' => 'forum/thread', //make sure this is listed first so it has priority
'thread/<id:\d+>' => 'forum/thread',
Then in your controller you would have this:
public function actionThread($id,$title=null) {
//$title will contain title from url if sent
}
Try this:
'forum/any-value/thread/<id:\d+>' => 'any-value/forum/thread',
and with this:
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))
So you should get forum/any-value/thread/2
that should work!
But If you are inside the module called forum then you would do like that:
'any-value/thread/<id:\d+>' => 'any-value/forum/thread',
and with this:
createUrl('any-value/forum/thread', array('id'=>$value->thread_id))

CGridview filter on page load with pre define value in search field

I am working with the Yii framework.
I have set a value in one of my cgridview filter fields using:
Here is my jQuery to assign a value to the searchfield:
$('#gridviewid').find('input[type=text],textarea,select').filter(':visible:first').val('".$_GET['value']."');
And here my PHP for calling the cgridview:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'bills-grid',
'dataProvider'=>$dataProvider,
'filter'=>$model,
'cssFile'=>Yii::app()->baseUrl . '/css/gridview.css',
'pager'=>array(
'class'=>'AjaxList',
'maxButtonCount'=>25,
'header'=>''
),
'columns' => $dialog->columns(),
'template'=>"<div class=\"tools\">".$dialog->link()." ".CHtml::link($xcel.' Export to excel', array('ExcelAll'))."</div><br />{items}{summary}<div class=\"pager-fix\">{pager}</div>",));
The value appears in the search field and my cgridview works correctly without any issues, but I am unable to trigger the cgridview to refresh or filter. Does anyone know who to trigger the cgridview to filter after page load with a predefined value?
Any help would be greatly appreciated and please let me know if you need additional information.
Thank you.
You can solve the problem without any clientside code modification. In your controller action just set the default value for the attribute as shown below
public function actionAdmin()
{
$model = new Bills();
$model->unsetAttributes();
$model->attribute_name="default filter value";//where attribute_name is the attribute for which you want the default value in the filter search field
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
$this->render('admin',array('model'=>$model));
}
Have a look at 'default' index action that gii generates:
public function actionIndex()
{
$model = new Bills();
$model->unsetAttributes();
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
$this->render('index',array('model'=>$model));
}
So if you add one line like: $model->attribute = 'test';, you're done. 'attribute' is of course the attribute that has to have the default filter value (in this case value is 'test') :). So your code looks like:
public function actionIndex()
{
$model = new Bills();
$model->unsetAttributes();
if(isset($_GET['Bills'])){
$model->attributes = $_GET['Bills'];
}
if(!isset($_GET['Bills']['attribute']) {
$model->attribute = 'test';
}
$this->render('index',array('model'=>$model));
}
Of course youre attribute will have a test value (in filter) set up as long as you wont type anything in its filter field. I hope that that's what you're looking for. Your filter should work as always.
Sorry for my bad english :)
Regards
You can use Yii's update:
$.fn.yiiGridView.update('bills-grid', {
type: 'GET',
url: <?php echo Yii::app()->createUrl('controller/action') ?>"?Class[attribute]=<?php echo $_GET['value'] ?>
success: function() {
$.fn.yiiGridView.update('bills-grid');
}
});
This is how i do it, just change the URL, it should be the same controller action of the gridview and change URL parameters to the structure represented in there, should be like Bills[attribute]=value.

Data saved as null when using RadioButtonList or dropDownList

I'm new to yii. I have this problem where my data stored in radiobuttonlist or dropDownList is not saved in the database. It always shows as null. here's my code
View:
<?php
$form = $this->beginWidget('CActiveForm');
echo $form->label($model,'gender');
echo $form->radioButtonList($model,'gender',array('M'=>'Male','F'=>'Female'));
echo $form->label($model,'cat');
echo $form->dropDownList($model,'cat',$category);
echo CHtml::submitButton('Submit');
$this->endWidget();
?>
Controller:
public function actionCreate()
{
$model=new Test;
if(isset($_POST['Test']))
{
$model->attributes=$_POST['Test'];
if($model->save()){
$this->redirect(array('index'));
}
else
var_dump($model->errors);
}
$cat = array('st'=>'STAFF','ot'=>'OTHERS');
$model->gender='M';
$this->render('create',array(
'model'=>$model,'category'=>$cat
));
}
Kindly help... Thanks in advance
EDIT: After adding the required in the rule section it works like a charm
Well here's the modified Test model
public function rules()
{
return array(
array('gender,cat', 'required'),
array('name', 'length', 'max'=>45),
);
}
Post your model here.I think your problem is in Test model.
I see you solved it using 'required', but if there are some fields that are not mandatory, you can just use the 'safe' rule. The point is that every attribute of your form has to be on the rules of your model.
Have a look to Understanding "Safe" Validation Rules.