One form for the model and it's sub-models

Hi,

I’ve got an “Order” model which has two related fields, “invoice” and “shipping” - both are foreign keys referencing Address.ID

Now I’d like to edit all the 3 models (Order and two Addresses) on one form, so I render the main form and it’s two subforms, but how do I tell Yii to distinguish between “invoice address” and “shipping address”?




<?php $form=$this->beginWidget('bootstrap.widgets.BootActiveForm' ... ?>

	<?php echo $form->textFieldRow($model,'name',array('class'=>'span5','maxlength'=>255)); ?>

	<?php $this->renderPartial('application.views.admin._common._address', array('form' => $form

	)) ?>	

	<?php $this->renderPartial('application.views.admin._common._address', array('form' => $form

	)) ?>

...



and the _address:




	<?php echo $form->textFieldRow($submodel,'city',array('class'=>'span5','maxlength'=>100)); ?>

...

The problem is that Yii creates the “Address[city]” field two times, and I’d like to name it differently for Invoice and Shipping.

the partial solution (doesn’t support Ajax Validation, but it seems to be feature, not the bug :confused: )

the form




<?php $form=$this->beginWidget('bootstrap.widgets.BootActiveForm', ... ) ?>

	    <?php $this->renderPartial('application.views.admin._common._address', array(

	        'submodel' => $model->_invoice,

	        'name' => 'Invoice',

	        'form' => $form,

	    )) ?>



the "_address" form


<?php echo $form->textFieldRow($submodel,"[$name]code",array('class'=>'span5','maxlength'=>100)); ?>

<?php echo $form->textFieldRow($submodel,"[$name]city",array('class'=>'span5','maxlength'=>100)); ?>



the model




class ... 

{

    public $_invoice = null;

    public $_shipping = null;


	public function relations()

	{

		return array(

			'shipping_addr' => array(self::BELONGS_TO, 'Address', 'shipping'),

			'invoice_addr' => array(self::BELONGS_TO, 'Address', 'invoice'),

....


	public function afterFind()

	{

        $this->_shipping = $this->shipping_addr;

        $this->_invoice = $this->invoice_addr;

	    return parent::afterFind();

	}

	

	public function afterConstruct()

	{

        $this->_shipping = new Address();

        $this->_invoice = new Address();

	    return parent::afterConstruct();

	}	



the controller




	    $model->_shipping->attributes = $_POST['Address']['Shipping'];

	    $model->_invoice->attributes = $_POST['Address']['Invoice'];

            $v1 = $model->_shipping->validate();

            $v2 = $model->_invoice->validate();

            

	    if ($v1 && $v2 && $model->validate())	 

    			$model->save(false);