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!
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.
echo('There was some error!');
part with var_dump($model->getErrors());
to see the validation errors - DCoder 2012-04-05 13:54
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
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