Yii一般控件使用代码

View -------------------------------------------------

Php代码 复制代码 收藏代码
  1. <?php 
  2. $this->pageTitle=Yii::app()->name . ' - 总结'
  3. $this->breadcrumbs=array('控件使用总结',); 
  4. ?> 
  5.  
  6. <h1>控件使用总结</h1> 
  7.  
  8. <tr class="form"
  9. <?php $form=$this->beginWidget('CActiveForm', array
  10.     'id'=>'widget-form'
  11.     'enableClientValidation'=>true, 
  12.     'clientOptions'=>array
  13.     'validateOnSubmit'=>true, 
  14.     ), 
  15. )); ?> 
  16. <table> 
  17.     <!-- 文本框 --> 
  18.     <tr> 
  19.         <th><?php echo$form->labelEx($model,'textfiled'); ?></th> 
  20.         <td><?php echo$form->textField($model,'textfiled'); ?></td> 
  21.         <td><?php echo$form->error($model,'textfiled'); ?></td> 
  22.     </tr> 
  23.     <!-- 密码框 --> 
  24.     <tr> 
  25.         <th><?php echo$form->labelEx($model,'password'); ?></th> 
  26.         <td><?php echo$form->passwordField($model,'password'); ?></td> 
  27.         <td><?php echo$form->error($model,'password'); ?></td> 
  28.     </tr> 
  29.     <!-- 单复选框 --> 
  30.     <tr> 
  31.         <th><?php echo$form->labelEx($model,'checkBox'); ?></th> 
  32.         <td><?php echo$form->checkBox($model,'checkBox'); ?></td> 
  33.         <td><?php echo$form->error($model,'checkBox'); ?></td> 
  34.     </tr> 
  35.     <!-- 多复选框 --> 
  36.     <tr> 
  37.         <th><?php echo$form->labelEx($model,'checkBoxList'); ?></th> 
  38.         <td><?php echo$form->checkBoxList($model,'checkBoxList',WidgetForm::getCheckBoxListContent()); ?></td> 
  39.         <td><?php echo$form->error($model,'checkBoxList'); ?></td> 
  40.     </tr> 
  41.     <!-- 下拉列表 --> 
  42.     <tr> 
  43.         <th><?php echo$form->labelEx($model,'dropDownList'); ?></th> 
  44.         <td><?php echo$form->dropDownList($model, 'dropDownList', WidgetForm::getDropDownListContent()); ?></td>
  45.         <td><?php echo$form->error($model,'dropDownList'); ?></td> 
  46.     </tr> 
  47.     <!-- 单选按钮 --> 
  48.     <tr> 
  49.         <th><?php echo$form->labelEx($model,'radioButtonList'); ?></th> 
  50.         <td><?php echo$form->radioButtonList($model, 'radioButtonList', WidgetForm::getRadioButtonListContent());?></td> 
  51.         <td><?php echo$form->error($model,'radioButtonList'); ?></td> 
  52.     </tr> 
  53.          
  54.     <tr> 
  55.         <td><?php echo CHtml::submitButton('Submit'); ?></td>   
  56.     </tr> 
  57.  
  58. </table> 
  59.     <div class="row"
  60.         <?php  
  61.            if (isset($message)){ 
  62.                echo$message
  63.            } 
  64.         ?> 
  65.     </div> 
  66. <?php $this->endWidget(); ?> 
  67. </tr><!-- form --> 
<?php
$this->pageTitle=Yii::app()->name . ' - 总结';
$this->breadcrumbs=array('控件使用总结',);
?>

<h1>控件使用总结</h1>

<tr class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'widget-form',
    'enableClientValidation'=>true,
    'clientOptions'=>array(
    'validateOnSubmit'=>true,
    ),
)); ?>
<table>
    <!-- 文本框 -->
    <tr>
        <th><?php echo $form->labelEx($model,'textfiled'); ?></th>
        <td><?php echo $form->textField($model,'textfiled'); ?></td>
        <td><?php echo $form->error($model,'textfiled'); ?></td>
    </tr>
    <!-- 密码框 -->
    <tr>
        <th><?php echo $form->labelEx($model,'password'); ?></th>
        <td><?php echo $form->passwordField($model,'password'); ?></td>
        <td><?php echo $form->error($model,'password'); ?></td>
    </tr>
    <!-- 单复选框 -->
    <tr>
        <th><?php echo $form->labelEx($model,'checkBox'); ?></th>
        <td><?php echo $form->checkBox($model,'checkBox'); ?></td>
        <td><?php echo $form->error($model,'checkBox'); ?></td>
    </tr>
    <!-- 多复选框 -->
    <tr>
        <th><?php echo $form->labelEx($model,'checkBoxList'); ?></th>
        <td><?php echo $form->checkBoxList($model,'checkBoxList',WidgetForm::getCheckBoxListContent()); ?></td>
        <td><?php echo $form->error($model,'checkBoxList'); ?></td>
    </tr>
    <!-- 下拉列表 -->
    <tr>
        <th><?php echo $form->labelEx($model,'dropDownList'); ?></th>
        <td><?php echo $form->dropDownList($model, 'dropDownList', WidgetForm::getDropDownListContent()); ?></td>
        <td><?php echo $form->error($model,'dropDownList'); ?></td>
    </tr>
    <!-- 单选按钮 -->
    <tr>
        <th><?php echo $form->labelEx($model,'radioButtonList'); ?></th>
        <td><?php echo $form->radioButtonList($model, 'radioButtonList', WidgetForm::getRadioButtonListContent());?></td>
        <td><?php echo $form->error($model,'radioButtonList'); ?></td>
    </tr>
        
    <tr>
        <td><?php echo CHtml::submitButton('Submit'); ?></td>  
    </tr>

</table>
	<div class="row">
        <?php 
           if (isset($message)){
               echo $message ;
           }
        ?>
    </div>
<?php $this->endWidget(); ?>
</tr><!-- form -->

Form -------------------------------------------------

Php代码 复制代码 收藏代码
  1. <?php 
  2.  
  3. class WidgetForm extends CFormModel { 
  4.  
  5.     public$textfiled;    // 文本框 
  6.     public$password;    // 密码框 
  7.     public$dropDownList;    // 下拉列表 
  8.     public$checkBox;    // 单复选框 
  9.     public$checkBoxList;    //多复选框 
  10.     public$radioButtonList;    // 单选列表 
  11.  
  12.     publicfunction rules() { 
  13.         returnarray
  14.             array('textfiled, password, dropDownList, checkBox, checkBoxList, radioButtonList','safe'), 
  15.         ); 
  16.     } 
  17.  
  18.     publicfunction attributeLabels() { 
  19.         returnarray
  20.             'textfiled' => '文本框'
  21.             'password' => '密码框'
  22.             'dropDownList' => '下拉列表'
  23.             'checkBox' => '单选列表'
  24.             'checkBoxList' => '多复选框'
  25.             'radioButtonList' => '单选按钮'
  26.         ); 
  27.     } 
  28.  
  29.     publicstaticfunction getDropDownListContent(){ 
  30.         $result = array(); 
  31.         for($i=0 ; $i<10; $i++){ 
  32.             $result[$i]='DropDown'.$i
  33.         } 
  34.         return$result
  35.     } 
  36.  
  37.     publicstaticfunction getCheckBoxListContent(){ 
  38.         $result = array(); 
  39.         for($i=0 ; $i<3; $i++){ 
  40.             $result[$i]='CheckBox'.$i
  41.         } 
  42.         return$result
  43.     } 
  44.  
  45.     publicstaticfunction getRadioButtonListContent(){ 
  46.         $result = array(); 
  47.         for($i=0 ; $i<3; $i++){ 
  48.             $result[$i]='RadioButton'.$i
  49.         } 
  50.         return$result
  51.     } 
  52.      
<?php

class WidgetForm extends CFormModel {

    public $textfiled;    // 文本框
    public $password;    // 密码框
    public $dropDownList;    // 下拉列表
    public $checkBox;    // 单复选框
    public $checkBoxList;    //多复选框
    public $radioButtonList;    // 单选列表

    public function rules() {
        return array(
            array('textfiled, password, dropDownList, checkBox, checkBoxList, radioButtonList','safe'),
        );
    }

    public function attributeLabels() {
        return array(
            'textfiled' => '文本框',
            'password' => '密码框',
            'dropDownList' => '下拉列表',
            'checkBox' => '单选列表',
            'checkBoxList' => '多复选框',
            'radioButtonList' => '单选按钮',
        );
    }

    public static function getDropDownListContent(){
        $result = array();
        for($i=0 ; $i<10; $i++){
            $result[$i]='DropDown'.$i;
        }
        return $result;
    }

    public static function getCheckBoxListContent(){
        $result = array();
        for($i=0 ; $i<3; $i++){
            $result[$i]='CheckBox'.$i;
        }
        return $result;
    }

    public static function getRadioButtonListContent(){
        $result = array();
        for($i=0 ; $i<3; $i++){
            $result[$i]='RadioButton'.$i;
        }
        return $result;
    }
    
}

Controller -------------------------------------------------

Php代码 复制代码 收藏代码
  1. <?php 
  2.  
  3. class WidgetController extends Controller { 
  4.  
  5.     publicfunction actionIndex() { 
  6.         $model = new WidgetForm(); 
  7.  
  8.         if (isset($_POST['WidgetForm'])) { 
  9.             $model->attributes = $_POST['WidgetForm']; 
  10.              
  11.             $message = ""
  12.             $message = $message . "<br>文本框:" . getType($model->textfiled) . "  " . $model->textfiled; 
  13.             $message = $message . "<br>密码框:" . getType($model->password) . "  " . $model->password; 
  14.             $message = $message . "<br>单复选框:" . getType($model->checkBox) . "  " . $model->checkBox; 
  15.             $message = $message . "<br>多复选框:" . getType($model->checkBoxList) . "  " . print_r($model->checkBoxList,true); 
  16.             $message = $message . "<br>下拉列表:" . getType($model->dropDownList) . "  " . $model->dropDownList; 
  17.             $message = $message . "<br>单选列表:" . getType($model->radioButtonList) . "  " . $model->radioButtonList; 
  18.  
  19.             $this->render('index', array('model' => $model, 'message' => $message)); 
  20.         } else
  21.             $this->render('index', array('model' => $model)); 
  22.         } 
  23.     } 
  24.  
<?php

class WidgetController extends Controller {

    public function actionIndex() {
        $model = new WidgetForm();

        if (isset($_POST['WidgetForm'])) {
            $model->attributes = $_POST['WidgetForm'];
            
            $message = "";
            $message = $message . "<br>文本框:" . getType($model->textfiled) . "  " . $model->textfiled;
            $message = $message . "<br>密码框:" . getType($model->password) . "  " . $model->password;
            $message = $message . "<br>单复选框:" . getType($model->checkBox) . "  " . $model->checkBox;
            $message = $message . "<br>多复选框:" . getType($model->checkBoxList) . "  " . print_r($model->checkBoxList,true);
            $message = $message . "<br>下拉列表:" . getType($model->dropDownList) . "  " . $model->dropDownList;
            $message = $message . "<br>单选列表:" . getType($model->radioButtonList) . "  " . $model->radioButtonList;

            $this->render('index', array('model' => $model, 'message' => $message));
        } else{
            $this->render('index', array('model' => $model));
        }
    }

}
posted @ 2012-08-08 21:02  不悲伤的凄凉  阅读(260)  评论(0)    收藏  举报