phpformsvalidationactiverecordyii

yii active form always invalid


I have a problem with my yii project. In my admin panel I have a form for creating shop product made with CActiveForm widget. Something like this:

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

echo $form->errorSummary($model);

?>
<div class="row-fluid input-block-level-container">
    <div class="span12">
        <?php echo $form->labelEx($model,'name'); ?>

        <?php echo $form->textField($model,'name',array('size'=>60,'maxlength'=>255));      ?>
        <?php echo $form->error($model,'name'); ?>
        <?php if('help.name' != $help = Yii::t('crud', 'help.name')) { 
            echo "<span class='help-block'>{$help}</span>";            
} ?>
    </div>
</div>
... more fields in the same manner ...

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

My validation rules are:

    public function rules()
{
    return array_merge(
        parent::rules(), array(
        array('name, category_id, vendor_id', 'required'),
        array('description', 'safe'),
        array('category_id, vendor_id', 'numerical', 'integerOnly'=>true),
        array('name', 'length', 'max'=>255),
        array('id, name, category_id, vendor_id', 'safe', 'on'=>'search'),
        )
    );
}    

In my controller I have this line:

$model->attributes = $_POST['Product'];

which is supposed to populate model attributes with values from $_POST['Product']

But after the form is submitted I get an error that required fields are empty, although they are actually not. If in my controller I put something like this:

$model->name = $_POST['Product']['name'];
$model->category_id = $_POST['Product']['category_id'];
$model->vendor_id = $_POST['Product']['vendor_id'];

the forms is proccessed correctly. But ajax validation still says that the form is invalid. Appreciate any help!

EDIT: action code:

    public function actionCreate()
{
    $model = new Product;
    $model->scenario = $this->scenario;

            $this->performAjaxValidation($model, 'product-form');

    if(isset($_POST['Product'])) {

        $model->attributes = $_POST['Product'];

        if(isset($_POST['Product']['Attribute']))
            $model->setRelationRecords('attributes', $_POST['Product']['Attribute']);
        try {
            if($model->save()) {
                if (isset($_GET['returnUrl'])) {
                    $this->redirect($_GET['returnUrl']);
                } else {
                    $this->redirect(array('view','id'=>$model->id));
                }
            }
        } catch (Exception $e) {
            $model->addError('id', $e->getMessage());
        }
    } elseif(isset($_GET['Product'])) {
        $model->attributes = $_GET['Product'];
    }

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

    protected function performAjaxValidation($model)
{
    if(isset($_POST['ajax']) && $_POST['ajax']==='product-form')
    {
        echo CActiveForm::validate($model);
        Yii::app()->end();
    }
} 

Solution

  • My mistake was that I used reserved yii keywords as model properties and relation names. I have relation with name attributes. After renaming everything works fine.

    Thanks to everyone!