0

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

3 Answers 3

2
public function actionCreate(){
 if(!empty($_POST)) print_r($_POST);

.....

and you will see where you were wrong :)

Sign up to request clarification or add additional context in comments.

Comments

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 Comments

now i am able to get this form data in controller but it's not validating my data... can you take look of my controller pastebin.com/vgaC4wfi
i am using $_POST to get all data.. is is fine or i shall do like you suggested.. bcz if i follow your answer then i have to get data for every element and that would be take time...
1

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'); ?>

1 Comment

thanks but in my case designing is done by some frontend dev... so i can't do this way... but now i am able to get this form data in controller but it's not validating my data... can you take look of my controller pastebin.com/vgaC4wfi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.