Reputation: 1223
i have form which is written in pure Html tags...
<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'); ?>
now i want to save data in database using my controller method Create and i also want to validate it first... i don't know how to do it....
i know how to do it using in CActiveForm and CHtml
i have model class generated using Gii... and my controller method is...
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,
));
}
Can anybody suggest me some startup code for that...
thanks in advance
Upvotes: 0
Views: 6835
Reputation: 1305
Put the 'name' with the model class name like:
<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'); ?>
Upvotes: 1
Reputation: 11829
//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');
}
Upvotes: 1
Reputation: 31
public function actionCreate(){
if(!empty($_POST)) print_r($_POST);
.....
and you will see where you were wrong :)
Upvotes: 2