controller中创建一个actionHtml
public function actionHtml(){ //如果有表单提交,则给user对象赋值 if(!empty($_POST)){ //实例化user模型 $model = new User(); $now = time(); // dump($_POST); $model->username = $_POST['username']; $model->city_id = $_POST['city_id']; $model->create_time = $now; $model->update_time = $now; if(!$model->save()){ dump($model->getErrors()); }else{ dump('保存成功'); } } //渲染视图html并传入Model参数 $this->render('html'); }新建视图html.php
<div class="form"> <h1>添加新的用户</h1> <form method="POST"> <div class="row"> <label>用户名</label> <input type="text" name="username" value=""/> </div> <div class="row"> <label>城市</label> <select name="city_id"> <?php echo User::getCityOptions();?> </select> </div> <div class="row"> <input type="submit" value="添加"> </div> </form> </div>在User模型中定义了获取城市下拉选项的方法
public static function getCityOptions(){ $option = '<option value="">请选择</option>'; $citys = self::getCitys(); if(!empty($citys)){ foreach ($citys as $v=>$name) { $option.= "<option value={$v}>$name</option>"; } } return $option; } public static function getCitys(){ $citys = Yii::app()->db->createCommand()->select('id,name')->from('city')->queryAll(); if(!empty($citys)){ $citys = CHtml::listData($citys, 'id', 'name'); } return $citys; }
public function actionChtml(){ if(!empty($_POST)){ $now = time(); $model = new User(); //将post提交的值赋值给attributes属性 $model->attributes = $_POST; // dump($model->attributes); $model->create_time = $now; $model->update_time = $now; if(!$model->save()){ dump($model->getErrors()); }else{ dump('添加成功'); } } $this->render('chtml'); }view层
<div class="form"> <h1>添加新的用户</h1> <?php echo CHtml::beginForm('', 'POST');?> <div class="row"> <?php echo CHtml::label('用户名', 'username');?> <?php echo CHtml::textField('username', '');?> </div> <div class="row"> <?php echo CHtml::label('城市', 'city_id')?> <?php echo CHtml::dropDownList('city_id', '', User::getCitys())?> </div> <div class="row"> <?php echo CHtml::submitButton('添加')?> </div> <?php echo CHtml::endForm();?> </div>
public function actionActive(){ $model = new User(); if(!empty($_POST)){ $now = time(); // dump($_POST);//打印结果见图1 $model->attributes = $_POST['User']; $model->create_time = $now; $model->update_time = $now; if(!$model->save()){ dump($model->getErrors()); }else{ dump('添加成功'); } } $this->render('active',array( 'model'=>$model, )); }view中
<div class="form"> <h1>添加新的用户</h1> <?php echo CHtml::beginForm('', 'POST');?> <div class="row"> <?php echo CHtml::activeLabel($model, 'username');?> <?php echo CHtml::activeTextField($model, 'username');?> </div> <div class="row"> <?php echo CHtml::activeLabel($model, 'city_id');?> <?php echo CHtml::activeDropDownList($model, 'city_id', User::getCitys())?> </div> <div class="row buttons"> <?php echo CHtml::submitButton('添加')?> </div> <?php echo CHtml::endForm();?> </div>我们看到视图文件中的label显示的是 用户名和城市,而不是username和city_id,是因为他调用了User模型中的 attributeLabel方法
生成完成之后我们进入views/user目录下就会看到active_form文件
<div class="form"> <?php $form=$this->beginWidget('CActiveForm', array( 'id'=>'user-active_form-form', // Please note: When you enable ajax validation, make sure the corresponding // controller action is handling ajax validation correctly. // See class documentation of CActiveForm for details on this, // you need to use the performAjaxValidation()-method described there. 'enableAjaxValidation'=>false, )); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model,'username'); ?> <?php echo $form->textField($model,'username'); ?> <?php echo $form->error($model,'username'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'city_id'); ?> <?php echo $form->textField($model,'city_id'); ?> <?php echo $form->error($model,'city_id'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'create_time'); ?> <?php echo $form->textField($model,'create_time'); ?> <?php echo $form->error($model,'create_time'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'update_time'); ?> <?php echo $form->textField($model,'update_time'); ?> <?php echo $form->error($model,'update_time'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton('Submit'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->在控制器中我们需要做如下的配置
模仿gii提示的控制器方法,我们做如下代码
public function actionActiveform(){ $model=new User; // uncomment the following code to enable ajax-based validation /* if(isset($_POST['ajax']) && $_POST['ajax']==='user-active_form-form') { echo CActiveForm::validate($model); Yii::app()->end(); } */ if(isset($_POST['User'])) { $model->attributes=$_POST['User']; if($model->validate()) { // form inputs are valid, do something here return; } } $this->render('active_form',array('model'=>$model)); }