Yii一般控件使用代码

15 篇文章 0 订阅
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));
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值