[Solved]Invalid Request Only Sometimes

Good morning.

I have a create form for one model which a link to another create form inside it.

The link opens a CJuiDialog.

This works in some models and not others, but the code is the same. The code is:

Father model (TvEntradas) -> _form.php




...

echo CHtml::ajaxLink(Yii::t('Animal','Alta Animal'),$this->createUrl('TvAnimales/addNew'),

                                        array(

                                            'onclick'=>'$("#animal_dialog").dialog("open"); return false;',

                                            'update'=>'#animal_dialog'

                                            ),

                                        array(

                                            'id'=>'showAnimalDialog',

                                            'style'=>'color:#194407;',

                                        ));

...

<div id="animal_dialog"></div>

...



Son model (TvAnimales) createDialog.php




    $this->beginWidget('zii.widgets.jui.CJuiDialog',array(

                                        'id'=>'animal_dialog',

                                        'options'=>array(

                                            'title'=>Yii::t('Animal','Alta Animal'),

                                            'autoOpen'=>true,

                                            'modal'=>'true',

                                            'width'=>'auto',

                                            'height'=>'auto',

                                            'resizable'=>false,

                                        ),

                                        ));

    

    echo $this->renderPartial('_formDialog', array(

                                                'model'=>$model,

                                                'modelClasesAnimal'=>$modelClasesAnimal

                                                )); 


    $this->endWidget('zii.widgets.jui.CJuiDialog');

Son model _formDialog:




...

<?php 

        $form=$this->beginWidget('CActiveForm', array(

                        'id'=>'animal-form',

                        'enableAjaxValidation'=>true,

                        'clientOptions'=>array(

                            'validateOnSubmit'=>true,

                            'afterValidate'=>'js:function(form,data,hasError){

                                        if(!hasError){

                                                $.ajax({

                                                        "type":"POST",

                                                        "url":"'.CHtml::normalizeUrl(array("TvAnimales/addNew")).'",

                                                        "data":form.serialize(),

                                                        "success":function(data){

                                                                $("#TvEntradas_IdAnimal").append(data);

                                                                $("#animal_dialog").dialog("close");

                                                                },

                                                        });

                                                }

                                        }'

                                        ),

                            )); 

    ?>

...

<div class="row buttons">

                <?php echo CHtml::submitButton($model->isNewRecord ? 'Alta' : 'Actualizar'); ?>

            </div>

...



Son model controller:




...

public function actionAddNew()

    {

        $model=new TvAnimales;

        $modelClasesAnimal=new ToClasesAnimal;

        // Ajax Validation enabled

        $this->performAjaxValidation($model);

        // Flag to know if we will render the form or try to add 

        // new jon.

        $flag=true;

        

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

        {   

            $flag=false;

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

 

            if($model->save()) 

            {

                //Return an <option> and select it

                echo CHtml::tag('option',array(

                                                'value'=>$model->IdAnimal,

                                                'selected'=>true

                                                ),

                                CHtml::encode($model->CodigoDefinitivo),true);

            }

            else

            {

                return false;

            }

        }

                

        if($flag) 

        {

            Yii::app()->clientScript->scriptMap['jquery.js'] = false;

            //Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;

            $this->renderPartial('createDialog',array(

                                                    'model'=>$model,

                                                    'modelClasesAnimal'=>$modelClasesAnimal,

                                                    ),false,true);

        }

    }

...

This code works for other father and son models, but not in this.

I need help!!!

Thanks.

Solved!!!!

Really there was no problem…well, yes…MY HEAD!!!

In son controller I had this:




protected function performAjaxValidation($model)

    {

        if(isset($_POST['ajax']) && $_POST['ajax']==='to-propietarios-animal-form')

        {

            echo CActiveForm::validate($model);

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

        }

    }



And my form name was:




'id'=>'animal-form',



I change this for:




'id'=>'to-propietarios-animal-form',



and solved.