Hi!
I’m using MultiComplete extension.
And i can’t validate my widget by ajax , which use MultiComplete.
Form model
class ProfileEditForm extends CFormModel
{
public $name;
public $lastname;
public $experience;
public $about;
public $locCountry;
// related properties
public $musBands;
public $musBandToProfiles;
public $locCities;
public $musGenres;
public $musTypes;
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, lastname, experience, about, locCountry, locCities', 'required'),
array('locCountry', 'saveCountry'),
array('experience', 'numerical', 'integerOnly'=>true),
array('name, lastname', 'length', 'max'=>255),
array('about', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, user_id, name, lastname, experience, about', 'safe', 'on'=>'search'),
);
}
public function attributeLabels()
{
return array(
'locCountry' => 'Country',
);
}
public function saveCountry()
{
if(!empty($this->locCountry))
{
$matches = array();
preg_match_all('/[a-zA-Zа-яА-Яр-юР-Ю]+/', $this->locCountry, $matches);
$session = new CHttpSession;
$session->open();
$session['locCountry'] = $matches[0];
}
}
}
Controller (not save data yet)
class DefaultController extends Controller
{
public function actionIndex()
{
if(Yii::app()->getRequest()->getIsAjaxRequest())
{
$form = new ProfileEditForm;
echo CActiveForm::validate($form);
Yii::app()->end();
}
$session = new CHttpSession;
$session->open();
var_dump($session['locCountry']);
$form = new ProfileEditForm;
$this->render('profile_edit', array('model' => $form));
}
public function actionSearchCountry($term)
{
if(Yii::app()->request->isAjaxRequest && !empty($term))
{
$variants = array();
$criteria = new CDbCriteria;
$criteria->select='country_name';
$criteria->addSearchCondition('country_name',$term.'%',false);
$cities = LocCountry::model()->findAll($criteria);
if(!empty($cities))
{
foreach($cities as $city)
{
$variants[] = $city->attributes['country_name'];
}
}
echo CJSON::encode($variants);
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
}
}
form
<?php
/* @var $this ProfileEditFormController */
/* @var $model ProfileEditForm */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'profile-edit-form',
'enableAjaxValidation'=>true,
'enableClientValidation' => true
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'locCountry'); ?>
<?php $this->widget('application.components.MultiComplete', array(
'model'=>$model,
'attribute'=>'locCountry',
'splitter'=>',',
//'source'=>array('ac1', 'ac2', 'ac3'),
'sourceUrl'=>'profile/default/searchcountry',
// additional javascript options for the autocomplete plugin
'options'=>array(
'minLength'=>'2',
),
'htmlOptions'=>array(
'size'=>'60'
),
));
?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'experience'); ?>
<?php echo $form->textField($model,'experience'); ?>
<?php echo $form->error($model,'experience'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->textField($model,'name'); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lastname'); ?>
<?php echo $form->textField($model,'lastname'); ?>
<?php echo $form->error($model,'lastname'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'about'); ?>
<?php echo $form->textArea($model,'about'); ?>
<?php echo $form->error($model,'about'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
So validation work on widget only with POST. Other fields validate by ajax successful