I have an email field in my model registration form and other fields like name, country, age.
and I have to do an ajax validation onChange event only for email field .
I was set my view to enable ajax validation.
my problem is the ajax validation applied for all field not only for email, I need to do this action only for one field onChange event.
this my view
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'Login-Form',
'enableClientValidation'=>true,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
));
echo $form->errorSummary($loginForm);
echo $form->textFieldRow($loginForm, 'email');
echo $form->textFieldRow($loginForm, 'name');
echo $form->textFieldRow($loginForm, 'age');
echo $form->textFieldRow($loginForm, 'country');
and this is my model
public function rules()
{
return array(
array('email, country, name', 'required'),
array('email', 'checkEmail')
);
}
// custom function to check email
public function checkEmail($attribute, $params)
{
$this->addError($attribute,strtr('email exsit before',$params));
}
how can enable ajax validation only for email and the other filed (country, name, age) on client side without ajax validation.
Please help I send a lot of time to do that.
Thanks
To perform validation via ajax you first need to call a helper function in your controller in addition to you enabling the enableAjaxValidation
in your form view
In your Controller ( I am assuming the SiteController since this a login form )
<?php
class SiteController extends CController {
.... // Other functions
public function actionLogin(){ // Your corresponding action
$model= new LoginForm(); // Your actual model name
if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
echo CActiveForm::validate($model,array('email'));
Yii::app()->end();
}
... // Remaining action logic
see Validate and enableAjaxValidation api docs for full details how this works
Set the form clientOption on change like this:
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'Login-Form',
'enableClientValidation'=>true,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
So that on change it can trigger the validation This can be done by setting the 4th parameter to false on validate trigger.
<?php echo $form->error($model,'email', array(), false); ?>
In your model set this
public function rules()
{
return array(
array('email, country, name', 'required'),
array('email', 'unique'), //check if email already exist
array('email', 'email'),
);
}
in your controller
<?php
class SiteController extends CController {
.... // Other functions
public function actionLogin(){ // Your corresponding action
$model= new LoginForm(); // Your actual model name
$_POST['ajax'] === 'Login-form' make sure that login form is the same as your form name in view
if (isset($_POST['ajax']) && $_POST['ajax'] === 'Login-form') {
echo CActiveForm::validate($model,);
Yii::app()->end();
}
... // Remaining action logic
in your view
$form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'Login-Form',
'enableClientValidation'=>false,
'enableAjaxValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>false,
'validateOnChange'=>false,
'validateOnType' => true,