Site building and maintenance

Blog. Yii. Tips for Yii. Сhapter 3

Search

Tips for Yii. Сhapter 3.

  1. A grouped dropdown list.

    The first option:

    1. echo CHtml::dropDownList('Cars''car_id'array(  
    2.     'Toyota'=>array(  
    3.         1=>'Avensis',  
    4.         2=>'Camry',  
    5.     ),  
    6.     'Volvo'=>array(  
    7.         3=>'S60',  
    8.         4=>'XC70',  
    9.     ),  
    10. ));  

    The second option:

    1. $records = array(  
    2.     array('id' => 1, 'name' => 'Yii''group' => 'Framework'),  
    3.     array('id' => 2, 'name' => 'Drupal''group' => 'Framework'),  
    4.     array('id' => 3, 'name' => 'Joomla''group' => 'Framework'),  
    5.     array('id' => 4, 'name' => 'Сodeigniter''group' => 'Framework'),  
    6.     array('id' => 5, 'name' => 'Mercurial''group' => 'Version control system'),  
    7.     array('id' => 6, 'name' => 'Git''group' => 'Version control system'),  
    8.     array('id' => 7, 'name' => 'SVN''group' => 'Version control system'),  
    9. );  
    10.   
    11. $dropDownList = CHtml::listData($records'id''name''group');   
    12. echo CHtml::dropDownList('dropDownList'''$dropDownList);  

     

  1. Jquery does not work after renderPartial.

    For example, after clicking on a link, a new window opens in the FancyBox, and there is a datePicker on this page.

    To make a datePicker work, you need to write in the controller the following:

    1. if($isFancyBox){  
    2.     Yii::app()->clientscript->scriptMap['jquery.js'] = false;  
    3.     Yii::app()->clientscript->scriptMap['jquery.min.js'] = false;  
    4.     Yii::app()->clientscript->scriptMap['jquery-ui.min.js'] = false;  
    5.       
    6.     $this->renderPartial('form', array(  
    7.         'model' => $model,  
    8.     ), falsetrue);  
    9.       
    10. else{  
    11.     $this->render('form', array(  
    12.         'model' => $model,  
    13.     ));  
    14. }  

    scriptMap is an array which replaces the registered *.css or .js files by page rendering.

    To get it, please see one more example:

    1. Yii::app()->clientScript->scriptMap=array(  
    2.     'jquery.js'=>'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',  
    3.     'jquery.min.js'=>'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js',  
    4.     'style.css'=>'/css/all.css'  
    5. );  

     

  2. Expand our own application by multiple use of one and the same extension for actions of the same type; for example, to delete the entry.

    In the protected/extensions/ folder we create a new folder "test" with testActionDelele.php file in it which contains:

    1. <?php  
    2. class testActionDelele extends CAction {  
    3.     public $modelName;  
    4.     public $redirectAfter = array('index');  
    5.   
    6.     public function run($id = null) {  
    7.         if (id) {  
    8.             CActiveRecord::model($this->modelName)->deleteByPk($id);  
    9.         }  
    10.           
    11.         if(Yii::app()->getRequest()->getIsAjaxRequest()) {  
    12.             Yii::app()->end(200, true);  
    13.         }  
    14.         else {  
    15.             $this->getController()->redirect($this->redirectAfter);  
    16.         }  
    17.     }  
    18. }  

    Then we create a protected/controllers/testController.php controller:

    1. <?php  
    2. class testController extends CController {  
    3.     public function actions() {  
    4.         return array(  
    5.             'deleteUser' => array(  
    6.                 'class' => 'ext.test.testActionDelele',  
    7.                 'modelName' => 'User',  
    8.                 'redirectAfter' => array('indexUsers'),  
    9.             ),  
    10.             'deletePost' => array(  
    11.                 'class' => 'ext.test.testActionDelele',  
    12.                 'modelName' => 'Post',  
    13.                 'redirectAfter' => array('indexPosts'),  
    14.             ),  
    15.         );  
    16.     }  
    17.     ...  
    18. }  

    Now the user or the entry will be deleted while opening /test/deleteUser/{$id}/ or /test/deletePost/{$id}/ accordingly. After it - redirect to the 'redirectAfter' page which is specified in the key.

     

  3. Own validation functions - for example, captcha validator.

    We should write in the model:

    1. public function rules() {  
    2.     return array(  
    3.         ...  
    4.         array('verifyCode''required''on' => 'registration'),  
    5.         array('verifyCode''captchaValidate''on' => 'registration'),  
    6.     )  
    7. }     
    8.   
    9. public function captchaValidate() {  
    10.     $code = Yii::app()->controller->createAction('captcha')->getVerifyCode();  
    11.     if ($code != $this->verifyCode)  
    12.         $this->addError('verifyCode''Invalid captcha code');  
    13. }  

    By validating the captcha we should create the method captchaValidate, where we can "set our own result events".

    For example, we get the version of user's browser which we do not like. So we always output the message "Invalid captcha code".

     

Discuss the article in the forum