2013-03-01 98 views
4

我已經創建了以下嵌套表單數組;Yii CForm,嵌套表單Ajax驗證

return array(

    'elements' => array(
     'contact' => array(
      'type' => 'form', 
      'elements' => array(
       'first_name' => array(
        'type' => 'text', 
       ), 
       'last_name' => array(
        'type' => 'text', 
       ) 
      ), 
     ), 
     'lead' => array(
      'type' => 'form', 
      'elements' => array(
       'primary_skills' => array(
        'type' => 'textarea', 
       ), 
      ), 
     ), 
    ), 

    'buttons' => array(
     'save-lead' => array(
      'type' => 'submit', 
      'label' => 'Create', 
      'class' => 'btn' 
     ), 
    ) 
); 

我有鑑於這樣的

echo $form->renderBegin(); 
echo $form['lead']; 
echo $form['contact']; 
echo $form->buttons['save-lead']; 
echo $form->renderEnd(); 

我actionCreate頁是這樣

$form = new CForm('application.views.leads.register'); 
$form['lead']->model = new Lead; 
$form['contact']->model = new Contact; 

// how can i perform ajax validation only for $form['contact'] 
$this->performAjaxValidation($model); 

//if contact form save btn is clicked 
if ($form->submitted('save-lead') && $form['contact']->validate() && 

    $form['lead']->validate() 
) { 

    $contact = $form['contact']->model; 
    $lead = $form['lead']->model; 
    if ($contact->save()) { 
     $lead->contact_id = $contact->id; 
     if ($lead->save()) { 
      $this->redirect(array('leads/view', 'id' => $lead->id)); 
     } 
    } 
} 

AJAX驗證方法是

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

所以我的問題是我如何能在$ form ['c。]上執行ajax驗證ontact']和$ form ['lead']個別元素?

+0

你已經解決了這個了嗎?這裏有同樣的問題。 – MEM 2013-04-24 16:34:40

+0

您可以向提交按鈕添加一個名稱,並檢查它是否存在於您的控制器中。 ();} 然後在你的控制器中if(isset($ _ REQUEST ['submit-contact'])){echo CActiveForm :: validate($ contact); YII ::應用程序() - >端(); }' – dimvic 2013-11-25 00:33:43

回答

1

你可以在一個頁面中有幾種形式,但不應該嵌套。 嵌套表單無效。

0

你應該讓自己的驗證

在actionCreate和你的控制器,你必須添加的actionUpdate

(我有主力機型「發票」和輔「InvoiceDetails」,並有可能超過1種形式InvoiceDetails)。但當然形式不能嵌套!

 public function actionCreate() 
    { 
    ... 
    $PostVar = 'Invoices'; 
    if (Yii::app()->request->isAjaxRequest) 
       { // if ajax 
       $this->performAjaxValidation($model, strtolower($PostVar) . '-form'); 
       $PostVar = ucfirst($PostVar); 

       if (isset($_POST[$PostVar])) 
       { 
        $model->attributes = $_POST[$PostVar]; 
        $dynamicModel = new InvoiceDetails(); //your model 
        $valid = self::validate($model, $dynamicModel); 


        if (!isset($_POST['ajax'])) 
        { 

         if (isset($_POST['InvoiceDetails'])) 
         { 
          $allDetails = array(); 
          $allDynamicModels = $_POST['InvoiceDetails']; 

          //your own customization 
          foreach ($allDynamicModels as $key => $value) 
          { 
           $InvDet = InvoiceDetails::model()->findByPk($_POST['InvoiceDetails'][$key]['id']); 
           if (!isset($InvDet)) 
           { 
            $InvDet = new InvoiceDetails(); 
           } 
           $InvDet->attributes = $_POST['InvoiceDetails'][$key]; 
           $InvDet->save(); 
           $allDetails[] = $InvDet; 
          } 
         } 

         $model->invoicedetails = $allDetails; 

         if ($model->save()) 
         { 
          echo CJSON::encode(array('status' => 'success')); 
          Yii::app()->end(); 
         } 
         else 
         { 
          echo CJSON::encode(array('status' => 'error saving')); 
          Yii::app()->end(); 
         } 
        } 
        // Here we say if valid 
        if (!isset($valid)) 
        { 
         echo CJSON::encode(array('status' => 'success')); 
        } 
        else 
        { 
         echo $valid; 
        } 

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


       } 
       else 
       { 

        $this->renderPartial('_form', ...); 

       } 
      } 
      else 
      { 
       // not AJAX request 

       $this->render('_form', ...)); 


      } 
0

嵌套表格無效。您可以使用scenarios在不同的實例驗證表單。

例子:

`if ($form->submitted('save-lead'){ 
    $form->scenario = 'save-lead'; 
    if($form->validate()) { 
    $form->save(); 
    } 
} else { 
    $form->scenario = 'contact'; 
    if($form->validate()){ 
     $form->save(); 
    } 
} 
$this->render('_form', array('form'=>$form);`