Collect data depending on the value of a checkbox.

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:



public function actionCreate()


	$banco = new Banco;		

	$direccion = new Direccion;




		$banco->attributes = $_POST['Banco'];

		$direccion->attributes = $_POST['Direccion'];


		$valid = $banco->validate() && $direccion->validate();


		if ($valid) {


			$banco->banco_id_direccion = $direccion->direccion_id;







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 class="yiiForm">


Los campos con <span class="required">*</span> son obligatorios.


<?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 class="simple">

<?php echo CHtml::activeLabelEx($banco,'banco_comentarios'); ?>

<?php echo CHtml::activeTextField($banco,'banco_comentarios',array('size'=>60,'maxlength'=>255)); ?>


<hr />

<div class="simple">

<?php echo CHtml::activeLabelEx($direccion,'direccion_calle'); ?>

<?php echo CHtml::activeTextField($direccion,'direccion_calle',array('size'=>60,'maxlength'=>50)); ?>


<div class="simple">

<?php echo CHtml::activeLabelEx($direccion,'direccion_numero'); ?>

<?php echo CHtml::activeTextField($direccion,'direccion_numero',array('size'=>60,'maxlength'=>50)); ?>


<div class="simple">

<?php echo CHtml::activeLabelEx($direccion,'direccion_localidad'); ?>

<?php echo CHtml::activeTextField($direccion,'direccion_localidad',array('size'=>60,'maxlength'=>50)); ?>


<div class="simple">

<?php echo CHtml::activeLabelEx($direccion,'direccion_cp'); ?>

<?php echo CHtml::activeTextField($direccion,'direccion_cp',array('size'=>60,'maxlength'=>50)); ?>


<div class="simple">

<?php echo CHtml::activeLabelEx($direccion,'direccion_provincia'); ?>

<?php echo CHtml::activeTextField($direccion,'direccion_provincia',array('size'=>60,'maxlength'=>50)); ?>


<div class="simple">

<?php echo CHtml::activeLabelEx($direccion,'direccion_pais'); ?>

<?php echo CHtml::activeTextField($direccion,'direccion_pais',array('size'=>60,'maxlength'=>50)); ?>


<!---div class="simple">

<?php //echo CHtml::activeLabelEx($banco,'banco_id_direccion'); ?>

<?php //echo CHtml::activeTextField($banco,'banco_id_direccion'); ?>


<hr />

<div class="action">

<?php echo CHtml::submitButton('Agregar'); ?> <?php echo CHtml::resetButton('Reset'); ?>



</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?

Then do it. ;)

You don't need activeCheckBox. A regular CHtml::checkBox() is sufficient. In your action, check whether this checkbox is checked or not.

Awesome.  :o

Yii is really great.  ;D

Thanks for the fast answer.  :D

Another cuestion, How I can block or unblock the CHtml::activeTextField()?

What do you mean "block", "unblock"?

Or visible/invisible or lock/unlock.


Or visible/invisible or lock/unlock.

maybe so:


echo CHtml::activeTextField($form,'input-name', array('disabled' => 'true'))