Yii2:提交具有两个相同模型实例的表单验证

Follow up to: Yii2: Validation in form with two instances of same model

I have a model Booking with the fields shipping_address and billing_address, both of the model Address. I now want to print them to the same form which basically works pretty well. The validation works on filling the fields with data, but it fails on submitting the form.

In the controller, the billing address is set with the fields of an existing Address model and the shipping address is set with a new Address model:

 public function actionCreate()
{

    $model = new Donation();

    if (!empty($_POST['Address'])) {

        //TODO: Save models

        return $this->redirect(['view', 'id' => $model->id]);

    } else {

        $user = User::find(Yii::$app->user->identity->id)->one();
        $billing_address = $user->getBillingAddress()->one();

        return $this->render('create', [
            'model' => $model,
            'billing_address' => $billing_address,
            'shipping_address' => new Address()
        ]);
    }
}

The following code shows how the fields are printed.

Billings-fields:

<?= $form->field(
        $billing_address,
        'address_line_1',
        [
            'selectors' => [
                'input' => '#billing-address_line_1',
                'error' => '#billing-address_line_1',
                'container' => '.billing-address_line_1'
            ],
            'options' =>
                ['class' => 'billing-address_line_1']
        ])->textInput([
            'maxlength' => 45,
            'name'=> 'Billing_Address[address_line1]',
            'id'=>'billing-address_line_1',
    ]); ?>

Shipping-fields:

<?= $form->field(
        $shipping_address,
        'address_line_1',
        [
            'selectors' => [
                'input' => '#shipping-address_line_1',
                'error' => '#shipping-address_line_1',
                'container' => '.shipping-address_line_1'
            ],
            'options' =>
                ['class' => 'shipping-address_line_1']
        ])->textInput([
                'maxlength' => 45,
                'name'=> 'Shipping_Address[address_line1]',
                'id'=>'shipping-address_line_1',
    ]); ?>

As the following screenshot shows, the validation works on filling out the form:

enter image description here

The problem now - it doesn't work when i click the create button:

enter image description here

Any ideas on how i could solve this? What is the difference between the validation on filling out and the validation on clicking the button?

I would do so, I would not do the creation of the second address directly in rendering function but rather would create the first model and then would pass to the function.

 } else {

      $user = User::find(Yii::$app->user->identity->id)->one();
      $billing_address = $user->getBillingAddress()->one();
      $shipping_address = new Address();


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