Yii Framework Forum: Perform Ajaxvalidation With 2 Models, One With Multiple Instance - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Perform Ajaxvalidation With 2 Models, One With Multiple Instance Rate Topic: -----

#1 User is offline   nath-0 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 12-October 12
  • Location:switzerland

Posted 09 November 2012 - 03:33 AM

hello,

SomeOne know how to perform ajaxValidation whit 2 models, one has multiple instance.
http://www.yiiframew...-the-same-form/

View:

	<div class="row">
		<?php echo $form->labelEx($model,'tri'); ?>
		<?php echo $form->textField($model,'tri'); ?>
		<?php echo $form->error($model,'tri'); ?>
	</div>

	<div class="row">
		<?php echo $form->labelEx($model,'fk_groupe'); ?>
        <?php echo $form->dropDownList($model,'fk_groupe', CHtml::listData(AdGroupe::model()->findAll(), 'groupe_id', 'description')); ?>
	</div>

 <?php 
  $Langues = baLangue::model()->findAll(array('order'=>'langue_id'));
  $i=-1;
    foreach ($Langues as $Langue) {
      $i +=1;
        ?>
        <div class="row">
            <?php echo $form->labelEx($model,yii::t('view','nom').' ['.$Langue->langue_nom.']'); ?>
            <?php echo $form->textField($modelProfilLangue,'['.$i.']nom'); ?>
            <?php echo $form->error($modelProfilLangue,'['.$i.']nom'); ?>
             
        </div>
        
 <?php
 } 
 ?>


Controller :

public function actionCreate()
	{
		$model=new AdProfil;
        $modelProfilLangue=new AdProfilLangue;

		// Uncomment the following line if AJAX validation is needed
		$this->performAjaxValidation($model,$modelProfilLangue);

        
        if(isset($_POST['AdProfil']))
		{
			$model->attributes=$_POST['AdProfil'];
                                 
            $transaction=$model->dbConnection->beginTransaction();
            // pour avoir les langues dans le meme ordre que sur la form
            $Langues = baLangue::model()->findAll(array('order'=>'langue_id'));
            try
            {
               $model->profil_id = AdDroit::getNewId("Ad_profil", 'profil_id');
               $model->save();
                                        
               $i =-1;
               
               foreach($_POST['AdProfilLangue'] as $postAdProfilLangue) 
               {                  
                   $i +=1;                   
                   $modelProfilLangue=new AdProfilLangue;
                   //$modelProfilLangue->performAjaxValidation($modelProfilLangue);
                   $modelProfilLangue->attributes = $postAdProfilLangue; 
                   $modelProfilLangue->fk_langue = $Langues[$i]->langue_id;
                   $modelProfilLangue->fk_profil = $model->profil_id;
                   $modelProfilLangue->save();
               }
                $transaction->commit();
                Yii::app()->user->setFlash('messageToDisplay', yii::t('messsages','L\'enregistrement a été effectué!'.$str));
				$this->redirect(array('admin'));
            }
            catch (Exception $e)
            {
                $transaction->rollback();
                //Yii::app()->user->setFlash('messageToDisplay',yii::t('messsages','Probleme update!'));	
                Yii::app()->user->setFlash('messageToDisplay',$e->getMessage());
            }
            
		}
        

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


Thank you

Nath
0

#2 User is offline   nath-0 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 81
  • Joined: 12-October 12
  • Location:switzerland

Posted 09 November 2012 - 09:08 AM

at this point, I have a error message :

Quote

<pre>Array
(
[0] => Nom ne peut être vide.(name can not be empty)
)
</pre>{"AdProfilLangue_nom":["Nom ne peut \u00eatre vide."]}


But I still can not have the message in my view.


    protected function performAjaxValidation2($modelProfil, $attributes=null, $loadInput=true)
    {
       
        if(isset($_POST['ajax']) && $_POST['ajax']==='ad-profil-form')
        {
             $result=array();
            if($loadInput && isset($_POST[get_class($modelProfil)]))
				$modelProfil->attributes=$_POST[get_class($modelProfil)];  
            $models=array($modelProfil);
        
            if (isset($_POST['AdProfilLangue']))
            {
                foreach($_POST['AdProfilLangue'] as $postAdProfilLangue) 
               {  
                    $modelProfilLangue=new AdProfillangue;
                    $modelProfilLangue->attributes=$postAdProfilLangue;
                    array_push($models,$modelProfilLangue);    
               }               
            }
        
            foreach($models as $model)
            {
                $model->validate($attributes);
                foreach($model->getErrors() as $attribute=>$errors)
                    $result[CHtml::activeId($model,$attribute)]=$errors;
            }    
            echo '<pre>';
            print_r($result[CHtml::activeId($model,$attribute)]);
            echo '</pre>';
            
            return function_exists('json_encode') ? json_encode($result) : CJSON::encode($result);         
        }
     }
    
   protected function performAjaxValidation($modelProfil, $attributes=null, $loadInput=true)
  {
       
        if(isset($_POST['ajax']) && $_POST['ajax']==='ad-profil-form')
        {
            echo $this->performAjaxValidation2($modelProfil);
			Yii::app()->end();
        }
   }


any ideas to help me ?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users