Difference between #2 and #3 of
Creating a dependent dropdown

Revision #3 has been created by raphi on Jun 9, 2010, 12:31:39 PM with the memo:

Added Info about accessRules(), changed 'url'=>'dynamiccities' to 'url'=>CController::createUrl('currentController/dynamiccities')
« previous (#2) next (#4) »

Changes

Title unchanged

Creating a dependent dropdown

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

Content changed

[...]
```php
echo CHtml::dropDownList('country_id','', array(1=>'USA',2=>'France',3=>'Japan'),
array(
'ajax' => array(
'type'=>'POST', //request type
'url'=>
'CController::createUrl('currentController/dynamiccities'), //url to call.
 
//Style: CController::createUrl('currentController/methodToCall')

'update'=>'#city_id', //selector to update
//'data'=>'js:javascript statement'
[...]
You might wonder where the $_POST\['country_id'\] comes from. It's simple, when the 'data' key of the ajax array in the first dropdown is empty, all values of the elements of the form the dropdown is in, will be passed to the controller via the ajax request. If you're using Firebug you can inspect this request and see for yourself.

This behaviour can also be changed. By default the value of the 'data' key in the ajax configuration array is `js:jQuery(this).parents("form").serialize()`. The preceding `js:` indicates to Yii that a javascript statement will follow and thus should not be escaped. So, if you change the 'data' key to something else preceded by 'js:' you can fill in your own statement. The same applies to the 'success' parameter.

 
 
For this to work you also need to edit the Method accessRules() (if available) in your current Controller. In this example we would change 
 
 
```php 
array('allow', // allow authenticated user to perform 'create' and 'update' actions
 
'actions'=>array('create','update'),
 
'users'=>array('@'),
 
),
 
```
 
to
 
 
```php 
array('allow', // allow authenticated user to perform 'create' and 'update' actions
 
'actions'=>array('create','update','dynamiccities'),
 
'users'=>array('@'),
 
),
 
```
 
in order to allow access for authenticated Users to our Method 'dynamiccities'. To allow access to eg any User or to use more complex rules please read more about accessRules and Authentication [here](http://www.yiiframework.com/doc/guide/topics.auth).
 
 
**Note:** For testing purposes you could also comment out
 
 
```php 
'accessControl', // perform access control for CRUD operations
 
```
 
in the filters() Method. This will disable all access controls for the controller. You should always re-enable it after testing.
83 2
92 followers
Viewed: 412 388 times
Version: 1.1
Category: Tutorials
Written by: dalip
Last updated by: Paul_Kish
Created on: Mar 23, 2009
Last updated: 9 years ago
Update Article

Revisions

View all history