Yii一般控件使用代码

View
-------------------------------------------------
<?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

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

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));
        }
    }

}

你可能感兴趣的:(PHP,yii)