model attributes error

Hi, I have a wierd problem which I am not able to figure out.

I have a form like this:




<div class="row">

<strong>Name</strong>

<input style="width: 100%;" value="" name="CategoriesForm[category_description][en][name]" id="CategoriesForm_category_description_en_name" type="text">                    </div>    


<div class="row">

<strong>Description</strong>

<textarea style="width: 100%;" rows="4" value="" name="CategoriesForm[category_description][en][description]" id="CategoriesForm_category_description_en_description"></textarea>                    

</div>        


<div class="row">


<strong>Title</strong>

<input style="width: 100%;" value="" name="CategoriesForm[category_description][en][title]" id="CategoriesForm_category_description_en_title" type="text">                    </div>           


<div class="row">

<strong>META Description</strong>

<textarea style="width: 100%;" rows="4" value="" name="CategoriesForm[category_description][en][meta_description]" id="CategoriesForm_category_description_en_meta_description"></textarea>                    

</div>              


<div class="row">

<strong>Alias (SEF URL)</strong>


<input style="width: 100%;" value="" name="CategoriesForm[category_description][en][sef_url]" id="CategoriesForm_category_description_en_sef_url" type="text">

</div>          



My problem is when posting to the controller, I assign the $_POST[‘CategoriesForm’] to the $model->attributes but here is what I get when I try to output the results:




// using the default layout 'protected/views/layouts/main.php'

$model=new CategoriesForm;

        

// collect user input data

if(isset($_POST['CategoriesForm']))

{

    

    // pass form input values to the LoginForm class									

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

    echo '<pre>';

    

    print_r($_POST['CategoriesForm']);

    

    echo '</pre>';

    

    echo '<pre>';

    

    print_r($model->attributes);

    

    echo '</pre>';

    

    Yii::app()->end();	

}


Array

(

    [id] => 

    [category_description] => Array

        (

            [en] => Array

                (

                    [name] => aaaaaaaa

                    [description] => aaaaaaaaa

                    [title] => aaaaaaaaaa

                    [meta_description] => aaaaaaaaaa

                    [sef_url] => aaaaaaaaaaaaa

                )


            [fr] => Array

                (

                    [name] => 

                    [description] => 

                    [title] => 

                    [meta_description] => 

                    [sef_url] => 

                )


        )


)

Array

(

    [id] => 

    [category_description] => Array

        (

        )


)



I tried the same test with my login for and it works.

Anyone know why this happens?

Thanks!

You probably didn’t add validation rules to the model.

/Tommy

Here are my rules:




	public function rules()

	{

		$rules = array();

		

		foreach (Tbl_Language::model()->active()->findAll() as $value) {

			$rules[] = array('category_description['.$value->code.'][name]', 'required');

			$rules[] = array('category_description['.$value->code.'][description]', 'required');

			$rules[] = array('category_description['.$value->code.'][title]', 'required');			

			$rules[] = array('category_description['.$value->code.'][sef_url]', 'required');

		}		

		

		return $rules;

	}


Array

(

    [0] => Array

        (

            [0] => category_description[en][name]

            [1] => required

        )


    [1] => Array

        (

            [0] => category_description[en][description]

            [1] => required

        )


    [2] => Array

        (

            [0] => category_description[en][title]

            [1] => required

        )


    [3] => Array

        (

            [0] => category_description[en][sef_url]

            [1] => required

        )


    [4] => Array

        (

            [0] => category_description[fr][name]

            [1] => required

        )


    [5] => Array

        (

            [0] => category_description[fr][description]

            [1] => required

        )


    [6] => Array

        (

            [0] => category_description[fr][title]

            [1] => required

        )


    [7] => Array

        (

            [0] => category_description[fr][sef_url]

            [1] => required

        )


)




I’ve never tried something like this, but I think you can get the assignment to work by adding a CSafeValidator for the attribute ‘category_description’.




array('category_description', 'safe'),



(or assign explicitly to ‘category_description’ instead of to ‘attributes’).

/Tommy

One thing that works is if I do this:

Taken from the guide itself




foreach($_POST['CategoriesForm'] as $name=>$value)

{                   

	$model->$name=$value;

}		



But wouldn’t this just skip the safe attribute validation? Unless I do it in the loop, but I thought i’d try and find out why this way doesn’t work. I guess it’s because of the multi dimensional array maybe?

Thanks for answering though!

It is because of the multidimensional array… If you are editing one language at the time there is no need to use it…

Thanks for your reply!

Is there any way to fix this?