December 11 2014

Yii model rules dynamic required if extension

Tagged Under :

Yii
This extension is use to validate the field is required or not required depends by another field.

For Example, now the form have 2 fields, they are Name and Email. You can set if Name was not empty then Email field will become required to force the user enter the value.

In the Model rules you just include the extension file and then set the if value for which object only.
array('email', 'ext.requiredif', 'if' => 'name')

February 26 2014

Yii accessRules filter access in controllers for user roles.

Tagged Under :

Yii
I had install Rights module inside my application. The Rights module itself wouldn’t auto integrate with the controller.

So I need to integrate myself, at each of the controller we need to add 2 extra function they are:
public function filters() {
  return array(
    'accessControl', 
  );
}
and
public function accessRules() {
  return array(
    array('allow',
          'action' => array('Index'),
          'users' => array('@'),
    ),
  );
}

January 09 2014

example of Yii autocomplete

Tagged Under : ,

Yii
Because I didn’t found any example of Yii autocomplete function. particularly showing how to fetched data via a server request.

For the widget function in view:
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
  'name' => 'username',
  'value' => $model->username,
  'sourceUrl' => array('user/getusername'),//path of the controller
  'options' => array(
    'minLength' => '1', // min chars to start search
    'select' => '' // when the value selected
  ),
));

Now, we start create the getusername action in the UserController file.

October 05 2013

Yii different controller return different error page

Tagged Under :

Yii
Because need develop an APi application with frontend and backend together.

Frontend without any interface and it just return JSON format to the user. But backend have interface to let user do the setting and update data.

In Yii main.php only allow set one error page. The solution is set the error page in controller class.

You can follow below example to write your own error page to each controller.

October 05 2013

Yii cgridview show first and last page in pagination

Tagged Under :

Yii
Default Yii cgridview pagination didn’t show the first and last page. But actually it was hidden but the CSS class, the easy method is change the CSS class to let it can show it.



how to do it? open pager.css file and find the code like below.

Controller error handler:
ul.yiiPager .first, ul.yiiPager .last {
    display: none;
}

June 23 2013

Yii CGridView auto increase row number

Tagged Under :

Yii
Below example showing you how to add the row number in cgridview widgets.

You just need add the following script inside the array columns.
array(
	'header' => 'No.',
	'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),

The completed example as below:

June 23 2013

Yii include/preload yiigridview javascript

Tagged Under :

Yii
Normally we include Javascript in our application in Yii is below script
Yii::app()->getClientScript()->registerScriptFile('jquery.js');	
But, now we need include yiigridview.js script file in our application. Because it was generate by Yii and it located at assets folder. So above example cannot used.

In here, we need to call getAssetManager() to get the assets folder url and folder name generated by Yii.
Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets'));
The above example will get the gridview path from the assets folder.

May 13 2013

Yii CGridView listdata with CArrayDataProvider and Pagination

Tagged Under :

Yii
Following last post CGridview listdata with CArrayDataProvider to list down the data with cgridview.

But it just can listing the records without the pagination. Use back last post example script and then modify the CArrayDataProvider as below example.

May 13 2013

Yii CGridView listdata with CArrayDataProvider

Tagged Under :

Yii
Yii, use CArrayDataProvider to listing out the array with CGridView widgets.

Below example are showing how to use it.
Data array:
$data = array();
$data[] = array('id' => 1, 'name' => 'name 1', 'age' => 22, 'date' => '1-Jan-2013');
$data[] = array('id' => 2, 'name' => 'name 2', 'age' => 25, 'date' => '1-Feb-2013');
$data[] = array('id' => 3, 'name' => 'name 3', 'age' => 22, 'date' => '1-Mac-2013');
$data[] = array('id' => 4, 'name' => 'name 4', 'age' => 28, 'date' => '1-Jan-2013');
$data[] = array('id' => 5, 'name' => 'name 5', 'age' => 23, 'date' => '1-Apr-2013');
$data[] = array('id' => 6, 'name' => 'name 6', 'age' => 29, 'date' => '1-May-2013');
$data[] = array('id' => 7, 'name' => 'name 7', 'age' => 21, 'date' => '1-Jan-2013');
$data[] = array('id' => 8, 'name' => 'name 8', 'age' => 20, 'date' => '1-Feb-2013');

March 10 2013

Yii row number in CGridView

Tagged Under :

Yii
In Yii CGridView it doesn’t provide absolute row number in all the pages.

The easy way is write the a calculation method inside the column to show the column number.