Site building and maintenance

Blog. Yii. Tips for Yii. Chapter 8

Search

Tips for Yii. Chapter 8.

  1. CMaskedTextField.

    We will use a CMaskedTextField widget for a convenient and demonstrative input of the value in a field.

    For example, for input of a phone number.

    yii-tips-8-01

    Write in the view:

    1. <div class="rowold">    
    2.     <?php echo $form->labelEx($model,'phone'); ?>    
    3.     <?php    
    4.         $this->widget('CMaskedTextField'array(    
    5.             'model' => $model,    
    6.             'attribute' => 'phone',    
    7.             'mask' => '+7-999-999-9999',    
    8.             'placeholder' => '*',    
    9.         ));    
    10.         ?>    
    11.     <?php echo $form->error($model,'phone'); ?>    
    12. </div>   

    A 'mask' is a mask for an input of the value.

    A 'placeholder' are the symbols before an input of the values by the user, it is '*' in our example.

    We will check the correctness of the inputted in the model value according to the regular expressions:

    1. array('phone''match''pattern' => '/^((\+?7)(-?\d{3})-?)?(\d{3})(-?\d{4})$/''message' => 'Incorrect field {attribute}'),  

    By the way, http://regexlib.com is a website with regular expressions for all occasions.

     

  2. statePersister.

    Sometimes it is necessary to store the state (value) of a variable, but you are lazy to create a separate table for it in the database or to write it into a file.

    A statePersiste will help you.

    It is very convenient and easy to use. Let's see the example:

    1. $data = Yii::app()->statePersister->load(); // get value    
    2. if (isset($data['count']))    
    3.     $data['count']++;    
    4. else    
    5.     $data['count'] = 1;    
    6.     
    7. Yii::app()->statePersister->save($data); // set value   
    >

    Values are saved in a protected/runtime/state.bin file.

     

  3. CListView.

    Let's see different "delicacies" of a widget CListView.

    We have a usual block:

    1. $this->widget('zii.widgets.CListView'array(    
    2.     'dataProvider'=>$dataProvider,    
    3.     'itemView'=>'_view',    
    4.     'itemsTagName' => 'ol',    
    5.     'itemsCssClass' => 'advertising-blocks',    
    6.     'sortableAttributes'=>array('id'),    
    7. ));  

    yii-tips-8-02

    Change the template of an output:

    1. $this->widget('zii.widgets.CListView'array(    
    2.     'dataProvider'=>$dataProvider,    
    3.     'itemView'=>'_view',    
    4.     'itemsTagName' => 'ol',    
    5.     'itemsCssClass' => 'advertising-blocks',    
    6.     'sortableAttributes'=>array('id'),  
    7.     'template' => '{pager} {sorter} {items} {sorter} {pager}',            
    8. ));   

    yii-tips-8-03

    Change the text "Sort by" -> "Sorting":

    1. $this->widget('zii.widgets.CListView'array(    
    2.             'dataProvider'=>$dataProvider,    
    3.             'itemView'=>'_view',    
    4.             'itemsTagName' => 'ol',    
    5.             'itemsCssClass' => 'advertising-blocks',    
    6.             'sortableAttributes'=>array('id'),  
    7.             'template' => '{pager} {sorter} {items} {sorter} {pager}',            
    8.             'sorterHeader' => 'Sorting:',   
    9.         ));  

    yii-tips-8-04

    Add the displaying of a short information:

    1. $this->widget('zii.widgets.CListView'array(    
    2.             'dataProvider'=>$dataProvider,    
    3.             'itemView'=>'_view',    
    4.             'itemsTagName' => 'ol',    
    5.             'itemsCssClass' => 'advertising-blocks',    
    6.             'sortableAttributes'=>array('id'),  
    7.             'template' => '{summary} {pager} {sorter} {items} {sorter} {pager}',              
    8.             'sorterHeader' => 'Sorting:',   
    9.         ));   

    yii-tips-8-05

    Change the output of a short information:

    1. $this->widget('zii.widgets.CListView'array(    
    2.             'dataProvider'=>$dataProvider,    
    3.             'itemView'=>'_view',    
    4.             'itemsTagName' => 'ol',    
    5.             'itemsCssClass' => 'advertising-blocks',    
    6.             'sortableAttributes'=>array('id'),  
    7.             'template' => '{summary} {pager} {sorter} {items} {sorter} {pager}',              
    8.             'sorterHeader' => 'Sorting:',  
    9.             'summaryText' => 'Show: {start} - {end}, total: {count}',             
    10.         ));  

    yii-tips-8-06

    Change the text, if a table is blank or nothing is found by the given criteria:

    1. $this->widget('zii.widgets.CListView'array(    
    2.             'dataProvider'=>$dataProvider,    
    3.             'itemView'=>'_view',    
    4.             'itemsTagName' => 'ol',    
    5.             'itemsCssClass' => 'advertising-blocks',    
    6.             'sortableAttributes'=>array('id'),  
    7.             'template' => '{summary} {pager} {sorter} {items} {sorter} {pager}',              
    8.             'sorterHeader' => 'Sorting:',  
    9.             'summaryText' => 'Show: {start} - {end}, total: {count}',             
    10.             'emptyText' => 'No found',  
    11.         ));   

    yii-tips-8-07

     

Discuss the article in the forum