can't create a record in php yii framework

Go To StackoverFlow.com

1

Hello guys I am creating a website with php and Yii framework. Right now I have created a admin module and created crud in this module, but it seems that I can't create a record. what I have got for now is:

    public function actionCreate()
    {
        $model=new GamesValidator;

        // Uncomment the following line if AJAX validation is needed
        $this->performAjaxValidation($model);
        /*
        if(isset($_POST['ajax']) && $_POST['ajax']==='games-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        */
        if(isset($_POST['GamesForm']))
        {
            die('GamesForm is set'); // just to see if GamesForm has some value! but website never shows this massege, it shows just new form, which is my problem.
/*
            $model->attributes=$_POST['GamesForm'];
            if($model->validate()){
                echo ('This is only a test');
                return;
            } else {
                echo ('There was some error!');
                return;
            }
*/
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }

but it doesn't show anything, website shows the form.php again like nothing was done at all. here is a little code from my view file:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'games-form',
    'enableAjaxValidation'=>true
)); ?>

<p class="note">Fields with <span class="required">*</span> are required.</p>

<?php echo $form->errorSummary($model); ?>
[..........................]

<div class="row buttons">
    <?php echo CHtml::submitButton('Submit'); ?>
</div>

<?php $this->endWidget(); ?>

</div><!-- form -->

sorry I can't post a full code it is too long.

so can you tell me what can be wrong? and how to check if validation model has some errors?

EDIT

showed the place where is the problem!

2012-04-04 20:10
by Irakli


3

Simple, you are missing $model->save() in your create() method:-

// you'll have to remove the die() of course, otherwise the rest of the code won't be executed
if($model->validate()){
        echo ('This is only a test');
        // the next line is important to save records, we are passing false because validation is already done
        $model->save(false);
        return;
    } else {
        echo ('There was some error!');
        return;
    }

Read more about the methods in CActiveRecord.

Edit:

To see the changes in your app, you will need to make a view for the newly created record. In yii auto generated code (with gii) it is done through CDetailView. To this view you can pass the model's instance.

2012-04-04 21:07
by bool.dev
let me know if you need further clarifications. With my current solution you should be able to see the changes in your db, but you still wont be able to see the change in your app, until you make a specific view for it - bool.dev 2012-04-04 21:09
I did as you wrote but the problem is same again: website doesn't show not "This is only a test" and not "There was some error!". It simple loaded new form as if I haven't posted anything. Can it be that form does not POST at all - Irakli 2012-04-05 13:36
@Irakli: replace the echo('There was some error!'); part with var_dump($model->getErrors()); to see the validation errors - DCoder 2012-04-05 13:54
in addition to what DCoder has said. Check post variables, just var_dump($_POST); to see that the POST array being sent is in fact GamesForm, as in see that $_POST['GamesForm'] is not empty. Also did you check the db? are the values getting saved now - bool.dev 2012-04-05 14:04
I did vardump($model->getErrors()); and it did nothing, but when I did vardump($POST); it showed this: array(3) { ["YIICSRF_TOKEN"]=> string(40) "74d9d4a7213c32a0901859856b73635d4b424615" ["GamesValidator"]=> array(47) { ["title"]=> string(61) "irakli .... So what it does mean - Irakli 2012-04-05 14:15
ok, ["YIICSRFTOKEN"]= ... is one field in the $POST, then ["GamesValidator"]=> array(47) { ["title"]=> ... is one array in your $POST , is there a ["GamesForm"] array also? i'll tell you about getErrors after tha - bool.dev 2012-04-05 14:19
no, it is only "GamesValidator", does it mean that the form name is ""GamesValidator"? and how to change it to "GamesForm" ? I don't think I did something wrong in my view, because I have set 'id'=>'games-form', as it is shown in my code above - Irakli 2012-04-05 14:22
form id is games-form, but the data being sent is in "GamesValidator" , so in your create() change the if(isset($_POST["GamesForm"])) to if(isset($_POST['GamesValidator'])). the name of the array is taken from the model automatically by the line echo $form->textField($model); hence the name is GamesValidator. Also change $model->attributes=$_POST["GamesValidator"]bool.dev 2012-04-05 14:26
but I don't want to change it to GamesValidator I want that the name was "GamesForm", so maybe I abused you and I am sorry, but if you know how to do it, please tell me. Will it be done if I simply change $model=new GamesValidator to $model=new GamesForm - Irakli 2012-04-05 14:34
let us continue this discussion in chatbool.dev 2012-04-05 14:37
Ads