我的表格是用纯Html标签写的......
<form method="post" action="<?php echo Yii::app()->getBaseUrl(true).'/index.php?r=user/create'?>">
<div class="row">
<label>Username</label><input type="text" name="username"/>
</div>
<div class="row">
<label>Password</label><input type="password" name="password"/>
</div>
<div class="row">
<label>Email</label><input type="text" name="email"/>
</div>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
现在我想使用我的控制器方法创建在数据库中保存数据,我还想先验证它...我不知道怎么做....
我知道如何在CActiveForm和CHtml中使用
我使用Gii生成模型类......我的控制器方法是......
public function actionCreate()
{
$model=new User;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['User']))
{
$model->attributes=$_POST['User'];
if($model->save())
$this->redirect(array('view','id'=>$model->UserId));
}
$this->render('create',array(
'model'=>$model,
));
}
有人可以为我推荐一些启动代码......
提前致谢
答案 0 :(得分:2)
public function actionCreate(){
if(!empty($_POST)) print_r($_POST);
.....
你会看到你错在哪里:)
答案 1 :(得分:1)
//controller
$request = Yii::app()->getRequest();
$model->username = $request->getPost('username');
$model->password = $request->getPost('password');
$model->save();
//view
if ($model->hasErrors('username')) {
echo $model->getError('username');
}
答案 2 :(得分:1)
将“名称”与模型类名称放在一起,如:
<form method="post" action="<?php echo Yii::app()->getBaseUrl(true).'/index.php?r=user/create'?>">
<div class="row">
<label>Username</label><input type="text" name="User[username]"/>
</div>
<div class="row">
<label>Password</label><input type="password" name="User[password]"/>
</div>
<div class="row">
<label>Email</label><input type="text" name="User[email]"/>
</div>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>