Tips for Yii. Chapter 8.
-
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.
Write in the view:
- <div class="rowold">
- <?php echo $form->labelEx($model,'phone'); ?>
- <?php
- $this->widget('CMaskedTextField', array(
- 'model' => $model,
- 'attribute' => 'phone',
- 'mask' => '+7-999-999-9999',
- 'placeholder' => '*',
- ));
- ?>
- <?php echo $form->error($model,'phone'); ?>
- </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:
- 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.
-
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:
- $data = Yii::app()->statePersister->load();
- if (isset($data['count']))
- $data['count']++;
- else
- $data['count'] = 1;
-
- Yii::app()->statePersister->save($data);
>
Values are saved in a protected/runtime/state.bin file.
-
CListView.
Let's see different "delicacies" of a widget CListView.
We have a usual block:
- $this->widget('zii.widgets.CListView', array(
- 'dataProvider'=>$dataProvider,
- 'itemView'=>'_view',
- 'itemsTagName' => 'ol',
- 'itemsCssClass' => 'advertising-blocks',
- 'sortableAttributes'=>array('id'),
- ));
Change the template of an output:
- $this->widget('zii.widgets.CListView', array(
- 'dataProvider'=>$dataProvider,
- 'itemView'=>'_view',
- 'itemsTagName' => 'ol',
- 'itemsCssClass' => 'advertising-blocks',
- 'sortableAttributes'=>array('id'),
- 'template' => '{pager} {sorter} {items} {sorter} {pager}',
- ));
Change the text "Sort by" -> "Sorting":
- $this->widget('zii.widgets.CListView', array(
- 'dataProvider'=>$dataProvider,
- 'itemView'=>'_view',
- 'itemsTagName' => 'ol',
- 'itemsCssClass' => 'advertising-blocks',
- 'sortableAttributes'=>array('id'),
- 'template' => '{pager} {sorter} {items} {sorter} {pager}',
- 'sorterHeader' => 'Sorting:',
- ));
Add the displaying of a short information:
- $this->widget('zii.widgets.CListView', array(
- 'dataProvider'=>$dataProvider,
- 'itemView'=>'_view',
- 'itemsTagName' => 'ol',
- 'itemsCssClass' => 'advertising-blocks',
- 'sortableAttributes'=>array('id'),
- 'template' => '{summary} {pager} {sorter} {items} {sorter} {pager}',
- 'sorterHeader' => 'Sorting:',
- ));
Change the output of a short information:
- $this->widget('zii.widgets.CListView', array(
- 'dataProvider'=>$dataProvider,
- 'itemView'=>'_view',
- 'itemsTagName' => 'ol',
- 'itemsCssClass' => 'advertising-blocks',
- 'sortableAttributes'=>array('id'),
- 'template' => '{summary} {pager} {sorter} {items} {sorter} {pager}',
- 'sorterHeader' => 'Sorting:',
- 'summaryText' => 'Show: {start} - {end}, total: {count}',
- ));
Change the text, if a table is blank or nothing is found by the given criteria:
- $this->widget('zii.widgets.CListView', array(
- 'dataProvider'=>$dataProvider,
- 'itemView'=>'_view',
- 'itemsTagName' => 'ol',
- 'itemsCssClass' => 'advertising-blocks',
- 'sortableAttributes'=>array('id'),
- 'template' => '{summary} {pager} {sorter} {items} {sorter} {pager}',
- 'sorterHeader' => 'Sorting:',
- 'summaryText' => 'Show: {start} - {end}, total: {count}',
- 'emptyText' => 'No found',
- ));
Discuss the article in the forum