-->

Yii Code Snippet Collection (Yii 1 Only) part 2

Setelah code snippet sesi yang pertama disini  berikut saya lanjutkan kembali code snippet yii bagian yang kedua semoga bermanfaat bagi temen - temen

Cara menggunakan Rules di YiiFramework

  1. Required
    <?php
    array('username, name, email, password', 'required'),
    array('reputation, id_city_FK, id_level_FK', 'numerical', 'integerOnly'=>true),
    array('username, password, salt_password, location', 'length', 'max'=>50),
    ?>
  2. Captcha
    <?php
    array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
    array('repeat_password','compare','compareAttribute'=>'password','message'=>'Password must be same'),
    ?>
  3. Unique
    <?php
    array('id_user','unique','message'=>'{attribute}:{value} already exists'),
    ?>
  4. Email
    <?php
    array('email', 'email','message'=>'The email isnLt correct'),
    ?>
    <?php
    $validator=new CEmailValidator;
    if(!$validator->validate($this->email)){
       $this->addError('Invalid email address');
    }
  5. Format Letter
    <?php
    array('question', 'filter', 'filter'=>'strtolower'),
    ?>

Cara setting bahasa di multi language YiiFramework

#MULTISITE
<?php
Yii::app()->language = 'zh-cn'; 
?>

Cara menggunakan URL di YiiFramework

#URL
<?php
/**
home url= /my_projects/index.php
base url= /my_projects
base path= E:\www\my_projects\protected
*/
 //1.
 Yii::app()->getHomeUrl();// /my_projects/index.php
 Yii::app()->getBaseUrl(); // /my_projects
 Yii::app()->getBaseUrl(true);// http://localhost/my_projects/index.php
 Yii::app()->getBasePath();
 Yii::app()->request->baseUrl;
 Yii::app()->request->basePath;
  //when using theme
 Yii::app()->theme->baseUrl;
?>

Cara menggunakan Widget di YiiFramework

  1.  Cara menggunakan CDetailView di YiiFramework
    <?php
    $this->widget('zii.widgets.CDetailView', array(
     'data'=>$model,
     'attributes'=>array(
      'title',             // title attribute (in plain text)
      'owner.name',        // an attribute of the related object "owner"
      'description:html',  // description attribute in HTML
      array(               // related city displayed as a link
       'label'=>'City',
       'type'=>'raw',
       'value'=>CHtml::link(CHtml::encode($model->city->name),
             array('city/view','id'=>$model->city->id)),
      ),
     ),
    ));
    ?>
  2. Cara menggunakan CJuiAutoComplete di YiiFramework
    Di sisi client view

    <?php 
    echo CHtml::script("
     function split(val) {
      return val.split(/,\s*/);
     }
     function extractLast(term) {
      return split(term).pop();
     }
     ")?>
    <?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
     'model'=>$model,
     'attribute'=>'tag',
     'source'=>"js:function(request, response) {
        $.getJSON('".$this->createUrl('suggest')."', {
          term: extractLast(request.term)
        }, response);
        }",
     'options'=>array(
       'delay'=>300,
       'minLength'=>2,
       'showAnim'=>'fold',
       'select'=>"js:function(event, ui) {
        var terms = split(this.value);
        // remove the current input
        terms.pop();
        // add the selected item
        terms.push( ui.item.value );
        // add placeholder to get the comma-and-space at the end
        terms.push('');
        this.value = terms.join(', ');
        return false;
      }"
     ),
     'htmlOptions'=>array(
       'size'=>'40'
     ),
    ));?>

     Disisi server

    <?php
    public function actionSuggest(){
     if (Yii::app()->request->isAjaxRequest && isset($_GET['term'])) {
      $models = Tag::model()->suggestTag($_GET['term']);
      $result = array();
      foreach ($models as $m)
       $result[] = array(
         'label' => $m->nama,
         'value' => $m->nama,
         'id' => $m->id_tag,
       );
     
      echo CJSON::encode($result);
     }
    }
    
    public function suggestTag($keyword){
      $tags=$this->findAll(array(
     'condition'=>'nama LIKE :keyword',
     'params'=>array(
       ':keyword'=>'%'.strtr($keyword,array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')).'%',
     )
      ));
      return $tags;
     }
    ?>
    
    
  3. Cara menggunakan CJuiDatePicker di YiiFramework
    <?php
    $this->widget('zii.widgets.jui.CJuiDatePicker',array(
     'attribute'=>'atribute',
     'model'=>$model,
     'value'=>$model->atribute,
    
     'options'=>array(
       'showAnim'=>'fold',
       'yearRange'=>'-70:+0',
       'changeMonth'=>'true',
       'changeYear'=>'true',
       'dateFormat' => 'yy-mm-dd', // save to db format
       'themeUrl' => Yii::app()->baseUrl.'/css/jui' ,
    
       'theme'=>'pool', //try 'bee' also to see the changes
    
       'cssFile'=>array('jquery-ui.css' /*,anotherfile.css, etc.css*/),
    
     ),
     'htmlOptions'=>array(
      'style'=>'height:20px;'
     ),
    )
    );
    ?>
Facebook Comments

0 komentar