title: Yii2: Validation in form with two instances of same model
body: <p>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 problem is the fields of both models have the same IDs. For example the field "zip" in the shipping_address has the id "zip" and the "zip" in billing_address also has "zip" as its id.</p>
  
  <p>If I now fill out the form, it validates both of the fields at the same time while only one of them is actually true.</p>
  
  <p><img src="https://i.stack.imgur.com/Yay6g.png" alt="enter image description here"><img src="https://i.stack.imgur.com/bNufc.png" alt="enter image description here"></p>
  
  <p>I've already changed the names of the fields to Shippping_Address['zip'] and Billing_Address['zip'] therefore the controller is able to recognize both models.</p>
  
  <p>Here's how the fields are printed:</p>
  
  <pre><code>&lt;?= $form-&gt;field($billing_address, 'address_line_1')-&gt;textInput(['maxlength' =&gt; 45, 'name'=&gt; 'Billing_Address[address_line1]']); ?&gt;
  &lt;?= $form-&gt;field($billing_address, 'address_line_2')-&gt;textInput(['maxlength' =&gt; 45, 'name'=&gt; 'Billing_Address[address_line2]']) ?&gt;
  &lt;?= $form-&gt;field($billing_address, 'zip')-&gt;textInput(['maxlength' =&gt; 11, 'name'=&gt; 'Billing_Address[zip]']) ?&gt;
  &lt;?= $form-&gt;field($billing_address, 'city')-&gt;textInput(['maxlength' =&gt; 45, 'name'=&gt; 'Billing_Address[city]']) ?&gt;
  &lt;?= $form-&gt;field($billing_address, 'country')-&gt;textInput(['maxlength' =&gt; 45, 'name'=&gt; 'Billing_Address[country]']) ?&gt;
  
  &lt;?= $form-&gt;field($shipping_address, 'address_line_1')-&gt;textInput(['maxlength' =&gt; 45, 'name'=&gt; 'Shipping_Address[address_line1]']); ?&gt;
  &lt;?= $form-&gt;field($shipping_address, 'address_line_2')-&gt;textInput(['maxlength' =&gt; 45, 'name'=&gt; 'Shipping_Address[address_line2]']) ?&gt;
  &lt;?= $form-&gt;field($shipping_address, 'zip')-&gt;textInput(['maxlength' =&gt; 11, 'name'=&gt; 'Shipping_Address[zip]']) ?&gt;
  &lt;?= $form-&gt;field($shipping_address, 'city')-&gt;textInput(['maxlength' =&gt; 45, 'name'=&gt; 'Shipping_Address[city]']) ?&gt;
  &lt;?= $form-&gt;field($shipping_address, 'country')-&gt;textInput(['maxlength' =&gt; 45, 'name'=&gt; 'Shipping_Address[country]']) ?&gt;
  </code></pre>
  
  <p>Edit: After implementing the answer of tony:</p>
  
  <pre><code>&lt;?= $form-&gt;field(
              $shipping_address,
              'address_line_1',
              [
                  'selectors' =&gt; [
                      'input' =&gt; '#shipping-address_line_1',
                      'container' =&gt; '.shipping-address_line_1'
                  ],
                  'options' =&gt;
                      ['class' =&gt; 'shipping-address_line_1']
              ])-&gt;textInput([
                  'maxlength' =&gt; 45,
                  'name'=&gt; 'Shipping_Address[address_line1]',
                  'id'=&gt;'shipping-address_line_1',
              ]); ?&gt;
  </code></pre>
  
  <p>I also changed the container class because it was the same as the one the other field had. Now the second field (with the code as shown above) is working perfectly. The problem is, the first field (with the code unchanged) is not validating anymore.</p>
  
  <p>How could this be solved?</p>