1

What I meet is almost like this:

  1. I have a create form, and the form contains two model's attribute;
  2. I pass them from controller to view, and add rules to validate attribute in both model;
  3. But the form validation did not work well - a model's validation is not work.

I have no idea about how to resolve this, thanks for help!

And I find a reference article - Complex Forms with Multiple Models, but it is TBD.


Here is my sample code.

Controller - 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
        ]);
    }
}

Model - 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'],
        ];
    }
}

View - 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(); ?>
1
  • Could you at least add the code for your controller's action and your model inputs? Without it your question is too broad. Commented Feb 11, 2015 at 14:18

2 Answers 2

1

This can be an option. You can try it by creating an custom model, like ContactForm something like this:

<?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 ',
        ];
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! But when the form needs many attributes, adding another model will bring lots of duplicate validation rules.
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,
    ]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.