Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii Ajax form validate only for one field

Tags:

jquery

php

yii

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

like image 573
Osama Jetawe Avatar asked Dec 03 '25 01:12

Osama Jetawe


1 Answers

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); ?>
like image 50
Sudhanshu Saxena Avatar answered Dec 05 '25 15:12

Sudhanshu Saxena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!