el código ajax es el siguiente
$('#cedula').on('blur', function(){ $.ajax({ url: <?php echo "'".CController::createUrl('solicitantes/ExistePersona')."'"; ?>, data: {'cedula' : $('#cedula').val()}, type: "post", success: function(data){ if (data == 0) { nombre.value = " "; nombre.disabled = false; apellido.value = " "; apellido.disabled = false; nacimiento.value = " "; nacimiento.disabled = false; sexo.value = " "; sexo.disabled = false; direccion.value = " "; direccion.disabled = false; numerocasa.value = " "; numerocasa.disabled = false; telefono.value = " "; telefono.disabled = false; email.value = " "; email.disabled = false; circuitos_id.value=" "; circuitos_id.disabled = false; $("#button").css("display", "block"); } else { var retrievedJSON = data; var array = JSON.parse(retrievedJSON); nombre.value = array[0].nombre; nombre.disabled = true; apellido.value = array[0].apellido; apellido.disabled = true; nacimiento.value = array[0].nacimiento; nacimiento.disabled = true; sexo.value = array[0].sexo; sexo.disabled = true; direccion.value = array[0].direccion; direccion.disabled = true; numerocasa.value = array[0].numerocasa; numerocasa.disabled = true; telefono.value = array[0].telefono; telefono.disabled = true; email.value = array[0].email; email.disabled = true; circuitos_id.value=array[0].circuitos_id; circuitos_id.disabled = true; $("#button").css("display", "none"); } } }); })
el actionExistePersona del controlador es el siguiente:
public function actionExistePersona () { if($_POST['cedula']) { $cedula = $_POST['cedula']; $datos = Yii::app()->db->createCommand("SELECT cedula, nombre, apellido, nacimiento, edad, sexo, direccion, numerocasa, telefono, email, circuitos_id FROM solicitantes WHERE cedula = ".$cedula)->queryAll(); echo(($datos)?json_encode($datos):''); } else { $datos = 0; echo(($datos)?json_encode($datos):''); } }
y el actionCreate del controlador es el siguiente:
public function actionCreate() { $model=new Solicitantes; $this->performAjaxValidation($model); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if(isset($_POST['Solicitantes'])) { $model->attributes=$_POST['Solicitantes']; $fechaNacimiento = $model->nacimiento; //Aqui puedes mandar a llamar un metodo que te calcule la edad p.e. $edad = $this->calcularEdad($fechaNacimiento); //Antes de guardar if ($edad != 0) { $model->edad = $edad; } else { } $model->usuarios_id = Yii::app()->user->id; if($model->save()) $this->redirect(array('view','id'=>$model->id)); } $this->render('create',array( 'model'=>$model, )); }