Hi everyone
Is there a way to give a CFormInputElement a value before it is rendered?
What I want to do is:
-
Get value from a previous form post (in my example, colegio_id)
-
Use this value to set CFormInputElement ‘colegio_id’
I’ve tried to set the attribute like so:
$alumno_form = new CForm('application.views.pagar.forma_buscar_alumno', $alumno_form_model);
$colegio_id = sprintf('%d', trim(strip_tags($_POST['datos']['colegio_id'])));
$alumno_form->colegio_id = $colegio_id;
But renders empty.
Is this possible?
Thanks!
I have this CFormModel:
class AlumnoBuscarForm extends CFormModel
{
public $colegio_id;
public $nombre;
public $buscar;
/**
* Reglas de validación
*/
public function rules()
{
return array(
// todos son obligatorias
array('nombre', 'required', 'message'=>Traductor::t('El campo "{attribute}" no puede ir vacío')),
// nombre_colegio debe ser minimo 3 caracteres
array('nombre', 'length', 'min'=>3, 'tooShort'=>Traductor::t('Por favor introduzca un texto válido para el nombre del alumno')),
);
}
/**
* Nombres de los campos/variables
*/
public function attributeLabels()
{
return array(
'nombre'=>Traductor::t('Nombre completo'),
'buscar'=>Traductor::t('Buscar'),
);
}
}
And this form:
return array(
'activeForm'=>array(
'class'=>'CActiveForm',
'id'=>'alumnos',
'stateful'=>true,
'enableClientValidation'=>true,
'enableAjaxValidation'=>false,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
'validateOnType'=>false,
),
),
'showErrorSummary'=>false,
'action'=>'index.php?r=pagar/buscarAlumnos',
'elements'=>array(
'colegio_id'=>array( //<-- this is the CFormInputElement I want to set
'type'=>'text'
),
'nombre'=>array(
'type'=>'text',
'class'=>'required',
'title'=>Traductor::t('Escriba nombre y apellidos del alumno. Puede usar mayúsculas o minúsculas. No use acentos ni caracteres especiales'),
'disabled'=>true,
'minlength'=>2,
'rel'=>'tooltip',
),
),
'buttons'=>array(
'buscar'=>array(
'type'=>'submit',
'attributes'=>array(
'value'=>Traductor::t('Buscar'),
'class'=>'submitRojo',
'ajax'=>array(
'beforeSend'=>"function() {
spinner('#alumnos_loading');
}",
'type'=>'POST',
'dataType'=> 'json',
'url'=>Yii::app()->createUrl('pagar/buscarAlumno'),
'success'=>'function(data) {
if (data.estatus==1) {
esconderErrores();
cargarListaAlumnos(data);
} else {
mostrarErrores(data);
}
}',
'complete'=>"function(data) {
spinner('#alumnos_loading');
}"
),
),
),
),
);
And it is rendered like so:
<?php echo $alumno_form->renderBegin() ?>
<?php echo $alumno_form['colegio_id']->renderInput() ?>
<div class="paso1">
<h3><?php echo Traductor::t('Buscar alumno') ?></h3>
<div>
<?php echo $alumno_form['nombre']->renderLabel() ?>
<?php echo $alumno_form['nombre']->renderInput() ?>
<?php echo $alumno_form['nombre']->renderError() ?>
</div>
</div><!-- .paso1 -->
<div class="botonesForm">
<?php echo $alumno_form->buttons['buscar'] ?>
<div class="clear"></div>
</div>
<?php echo $alumno_form->renderEnd() ?>
The action that creates everything is:
public function actionBusquedaAlumnos()
{
$alumno_form_model = new AlumnoBuscarForm;
$alumno_form = new CForm('application.views.pagar.forma_buscar_alumno', $alumno_form_model);
$colegio_id = sprintf('%d', trim(strip_tags($_POST['datos']['colegio_id'])));
$alumno_form->colegio_id = $colegio_id; //<-- here I try to set colegio id to the value from the POST, nothing is rendered
$this->render('buscar_alumnos', array(
'colegio'=>$colegio,
'alumno_form'=>$alumno_form
));
}