Hey guys, im having a bad time here with widgets loaded through Ajax and it’s validation… as you can see… im loading a widget from ajaxButton
BUTTON
$('body').on('click','#newEventBtn',function(){
$.ajax({
'type':'POST',
'data':{'fecha':fechaActual,'month':<?php echo $this->month ?>,'year':<?php echo $this->year ?>},
'url':'...?r=evento/createNuevoEvento', <- path was shorcut intentionally for this post
'cache':false,
'success':function(html){
$("#newEvento").html(html)} <-area where it loads the widget
});
return false;
});
the controller "createNuevoEvento only renderpartials a _view and this _view only instantiates the widget…
FORM WITHIN WIDGET’S VIEW
<?php
$form=$this->beginWidget('CActiveForm', array(
'id' => 'evento-form',
'enableClientValidation' => true,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions'=>array(
'enctype'=>'multipart/form-data'
),
));
?> <-only declaration parameters obviously
WIDGET CONTROLLER
public function run()
{
$usuario = Usuario::model()->getUsuarioSession();
$fecha = $this->year.'-'.$this->month.'-'.$this->fecha;
if(isset($_POST['ajax']) && $_POST['ajax']==='evento-form')
{
echo CActiveForm::validate($this->model);
Yii::app()->end();
}
/*if(Yii::app()->getRequest()->getIsAjaxRequest())
{
echo CActiveForm::validate( array($model));
Yii::app()->end();
} */
if(isset($_POST['Evento']))
{
// collects user input data
$this->model->attributes=$_POST['Evento'];
// validates user input and redirect to previous page if validated
if($this->model->validate()) {
$this->model->COMISION_idCOMISION = 9;
$this->model->USUARIO_owner = $usuario->id;
$fechaHoy = date('Y-m-d H:i:s');
$this->model->fechacreacion = $fechaHoy;
$this->model->fechahoraDelEvento = $fechaHoy;
//$model->fechacreacion = new DbExpression("NOW()");
$this->model->fueNotificado = 1;
//$model->fechahoraDelEvento = new DbExpression("NOW()");
$this->model->estado = 0;
$this->model->save();
//$this->render("_listaDia",array('arrayEventosPosibles' => $this->tiposEvento));
return;
}
}
// displays the form
$this->render("crearEventoThumb",array(
'model' => $this->model,
'arrayEventosPosibles' => $this->tiposEvento,
'fecha'=> $fecha));
}
}
?>
thing is: when instantiating the widget on any other view, works good… but when instantiating the widget inside the ajax-loaded view, doesn’t validate fields on change, and when submitting, switches to another view… what am i doing wrong? any other approach i could face to… im a Yii and AJAX kinda newbie…
thx in advantage…