2015-02-11 66 views
1

我遇到什麼幾乎是這樣的:Yii2:如何在視圖中使用多個模型時啓用表單驗證?

  1. 我有一個create form和形式包含兩個型號的屬性;
  2. 我將它們從控制器傳遞到視圖,並在兩個模型中添加規則以驗證屬性;
  3. 但表單驗證效果不佳 - 模型的驗證是 不起作用。

我不知道如何解決此問題,感謝您的幫助!

我找到一篇參考文章 - Complex Forms with Multiple Models,但它是待定。


這是我的示例代碼。

控制器 - SiteController.php:

namespace task\controllers; 

use yii\web\Controller; 

use task\models\Task; 
use task\models\Customer; 

class Task extends Controller 
{ 

    public function actionCreate() 
    { 
     $model = new Task; 
     $customerModel = new Customer; 

     // load, validate, save ... 

     return $this->render('create', [ 
      'model' => $model, 
      'customerModel' => $customerModel 
     ]); 
    } 
} 

型號 - Task.php,Customer.php:

namespace task\models; 

use yii\db\ActiveRecord; 

class Task extends AcitveRecord 
{ 
    public $title, $published_at; 

    public function rules() 
    { 
     return [ 
      [['title', 'published_at'], 'required'], 
      ['published_at', 'match', 'pattern' => '/patten for time/'] 
     ]; 
    } 
} 

namespace task\models; 

use yii\db\ActiveRecord; 

class Customer extends ActiveRecord 
{ 
    public $name; 

    public function rules() 
    { 
     return [ 
      ['name', 'required'], 
     ]; 
    } 
} 

視圖 - create.php:

<?php 

use yii\helpers\Html; 
use yii\widgets\ActiveForm; 

?> 

<?php $form = ActiveForm::begin(); ?> 

<?= $form->field($model, 'title')->textInput() ?> 

<?= $form->field($model, 'publish_at')->textInput() ?> 

<?= $form->field($customerModel, 'name')->textInput() ?> 

<?= Html::submitButton('submit')?> 

<?php ActiveForm::end(); ?> 
+0

你能至少添加代碼爲您的控制器的動作和模型的輸入?沒有它,你的問題太廣泛了。 – topher 2015-02-11 14:18:41

+0

謝謝,已添加。 – haoliang 2015-02-12 01:45:09

回答

1

這可能是一個選項。您可以通過創建一個自定義模式嘗試,像ContactForm是這樣的:

<?php 

namespace app\models; 

use Yii; 
use yii\base\Model; 

/** 
* CustomModel is the model behind the contact form. 
*/ 
class CustomModel extends Model 
{ 
    public $attributeFromModel1; 
    public $attributeFromModel2; 


    /** 
    * @return array the validation rules. 
    */ 
    public function rules() 
    { 
     return [ 
      // attributeFromModel1, attributeFromModel2 are required 
      [['attributeFromModel1', 'attributeFromModel2'], 'required'], 

      // ['email', 'email'], 
       ['attributeFromModel1','YourRule'] 
      // verifyCode needs to be entered correctly 
      ['verifyCode', 'captcha'], 
     ]; 
    } 

    /** 
    * @return array customized attribute labels 
    */ 
    public function attributeLabels() 
    { 
     return [ 
      'atttributeFromModel1' => 'Your Label', 
      'atttributeFromModel2' => 'Your Label ', 
     ]; 
    } 
+1

謝謝!但是當表單需要很多屬性時,添加另一個模型會帶來很多重複的驗證規則。 – haoliang 2015-02-12 02:07:01

1
public function actionUpdate($id) 
{ 
    $model = $this->findModel($id); 
    $customerModel = Customer::findOne($id); 

    if (!isset($model, $customerModel)) { 
     throw new NotFoundHttpException("The user was not found."); 
    } 

    $model->scenario = 'update'; 
    $customerModel->scenario = 'update'; 

    if ($model->load(Yii::$app->request->post()) && $customerModel->load(Yii::$app->request->post())) { 
     $isValid = $model->validate(); 
     $isValid = $customerModel->validate() && $isValid; 
     if ($isValid) { 
      $model->save(false); 
      $customerModel->save(false); 
      return $this->redirect(['view', 'id' => $id]); 
     } 
    } 

    return $this->render('update', [ 
     'model' => $model, 
     'customerModel' => $customerModel, 
    ]); 
} 
+0

請解釋你的代碼如何回答這個問題。 – AmazingDreams 2016-04-13 10:00:10

相關問題