Ok, I working with AR.
I have a form in wich I collect information of two AR one for a Bank, another for the Address of that Bank.
This work perfectly thank to the answer that i receive before. (http://www.yiiframew…opic,928.0.html)
This is my actual code:
<?php
...
public function actionCreate()
{
$banco = new Banco;
$direccion = new Direccion;
if(isset($_POST['Banco'],$_POST['Direccion']))
{
$banco->attributes = $_POST['Banco'];
$direccion->attributes = $_POST['Direccion'];
$valid = $banco->validate() && $direccion->validate();
if ($valid) {
$direccion->save();
$banco->banco_id_direccion = $direccion->direccion_id;
$banco->save();
$this->redirect(array('list',array('bancoList'=>$bancoList,'pages'=>$pages)));
}
}
$this->render('create',array('banco'=>$banco,'direccion'=>$direccion));
}
And this is my form:
<h2>Agregar Banco</h2>
<div class="actionBar">
[<?php echo CHtml::link('Lista de Bancos',array('list')); ?>]
[<?php echo CHtml::link('Administrar Bancos',array('admin')); ?>]
</div>
<div class="yiiForm">
<p>
Los campos con <span class="required">*</span> son obligatorios.
</p>
<?php echo CHtml::form(); ?>
<?php echo CHtml::errorSummary($banco); ?>
<?php echo CHtml::errorSummary($direccion); ?>
<div class="simple">
<?php echo CHtml::activeLabelEx($banco,'banco_nombre'); ?>
<?php echo CHtml::activeTextField($banco,'banco_nombre',array('size'=>60,'maxlength'=>50)); ?>
</div>
<div class="simple">
<?php echo CHtml::activeLabelEx($banco,'banco_comentarios'); ?>
<?php echo CHtml::activeTextField($banco,'banco_comentarios',array('size'=>60,'maxlength'=>255)); ?>
</div>
<hr />
<div class="simple">
<?php echo CHtml::activeLabelEx($direccion,'direccion_calle'); ?>
<?php echo CHtml::activeTextField($direccion,'direccion_calle',array('size'=>60,'maxlength'=>50)); ?>
</div>
<div class="simple">
<?php echo CHtml::activeLabelEx($direccion,'direccion_numero'); ?>
<?php echo CHtml::activeTextField($direccion,'direccion_numero',array('size'=>60,'maxlength'=>50)); ?>
</div>
<div class="simple">
<?php echo CHtml::activeLabelEx($direccion,'direccion_localidad'); ?>
<?php echo CHtml::activeTextField($direccion,'direccion_localidad',array('size'=>60,'maxlength'=>50)); ?>
</div>
<div class="simple">
<?php echo CHtml::activeLabelEx($direccion,'direccion_cp'); ?>
<?php echo CHtml::activeTextField($direccion,'direccion_cp',array('size'=>60,'maxlength'=>50)); ?>
</div>
<div class="simple">
<?php echo CHtml::activeLabelEx($direccion,'direccion_provincia'); ?>
<?php echo CHtml::activeTextField($direccion,'direccion_provincia',array('size'=>60,'maxlength'=>50)); ?>
</div>
<div class="simple">
<?php echo CHtml::activeLabelEx($direccion,'direccion_pais'); ?>
<?php echo CHtml::activeTextField($direccion,'direccion_pais',array('size'=>60,'maxlength'=>50)); ?>
</div>
<!---div class="simple">
<?php //echo CHtml::activeLabelEx($banco,'banco_id_direccion'); ?>
<?php //echo CHtml::activeTextField($banco,'banco_id_direccion'); ?>
</div--->
<hr />
<div class="action">
<?php echo CHtml::submitButton('Agregar'); ?> <?php echo CHtml::resetButton('Reset'); ?>
</div>
</form>
</div><!-- yiiForm -->
But now I want to store the Direccion only if the user wants to do that.
I think in put a checkbox in the form and, if the user checks it, then show the input fields for the data of a Direccion (by default hidden).
In others words I want to show (or unlock) the input fields for Direccion depending on the value of the checkbox.
This is necesary because some Bank have a Direccion but others no.
Any idea?