I’m having difficulty getting automatic client validation to work. I’m not the only person working on this project, so there’s a chance something has been damaged, removed, etc. without my knowledge. However, the framework itself should be intact.
In summary, this is what I have in the Controller for the action in question:
public function actionPresentation() {
$model = new PresentationForm;
$this->handleAjaxValidation($model);
if (isset($_POST['PresentationForm']))
{
$model->attributes = $_POST['PresentationForm'];
if ($model->validate()) {
$result = Mail::sendFormModel($model);
Yii::app()->user->setFlash('presentation',$result);
$this->refresh();
}
}
$this->render('presentation', array('model'=>$model));
}
// Incomplete
public function handleAjaxValidation($model) {
if (isset($_POST['ajax'])) {
echo $_POST['ajax'];
Yii::app()->end();
}
}
Here is what I have in the view.
<?php $form=$this->beginWidget('ActiveForm', array(
'id'=>'presentation-form-presentation-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>true,
'focus'=>array($model,'firstName'),
)); ?>
I load the page and focus works. In the source, the jquery script file is sourced, but not the yiiactiveform script file. No client validation seems to happen. Also, I have enableAjaxValidation on too, which I have not fully implemented in the Controller, but it does not seem to even try anyway. In the framework, I only find that yiiactiveform script mentioned once. It’s in the run() method of framework/web/widgets/CActiveForm.php. Here is the code:
public function run()
{
if(is_array($this->focus))
$this->focus="#".CHtml::activeId($this->focus[0],$this->focus[1]);
echo CHtml::endForm();
$cs=Yii::app()->clientScript;
if(!$this->enableAjaxValidation && !$this->enableClientValidation || empty($this->attributes))
{
if($this->focus!==null)
{
$cs->registerCoreScript('jquery');
$cs->registerScript('CActiveForm#focus',"
if(!window.location.hash)
$('".$this->focus."').focus();
");
}
return;
}
$options=$this->clientOptions;
if(isset($this->clientOptions['validationUrl']) && is_array($this->clientOptions['validationUrl']))
$options['validationUrl']=CHtml::normalizeUrl($this->clientOptions['validationUrl']);
$options['attributes']=array_values($this->attributes);
if($this->summaryID!==null)
$options['summaryID']=$this->summaryID;
if($this->focus!==null)
$options['focus']=$this->focus;
$options=CJavaScript::encode($options);
$cs->registerCoreScript('yiiactiveform');
$id=$this->id;
$cs->registerScript(__CLASS__.'#'.$id,"\$('#$id').yiiactiveform($options);");
}
I did some debugging and found that empty($this->attributes) ends up being true. This makes me wonder if I’m doing something wrong, but thus far have not found an answer, despite asking around.
So, any help would be greatly appreciated!