Ajax Validation doesn't work

I have an authorization form with enableAjaxValidation set true. Also clientOptions: validateOnSubmit => true, validateOnChange => false. But it doesn’t work.тывает.

Here’s the source.

Controller: SiteController


  function actionLogin() {

    if (Yii::app()->user->isGuest) {

      $model = new LoginForm;


      if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {

        echo CActiveForm::validate($model);

        Yii::app()->end;

      }


      if (isset($_POST['LoginForm'])) {

        $model->attributes = $_POST['LoginForm'];


        if ($model->validate() && $model->login()) {

          $this->redirect(Yii::app()->user->returnUrl);

        }

      }


      $this->layout = 'headerFooterOnly';

      $this->render('login', array('model' => $model));

    } else {

      $this->redirect($this->createUrl('site/index'));

    }

  } 

Model: LoginForm


class LoginForm extends CFormModel {


  public $username;

  public $password;

  public $remember_me;


  private $_identity;


  public function rules() {

    return array(

      array('username, password', 'required'),

      array('password', 'authenticate'),

    );

  }


  public function authenticate($attribute, $params) {

    $this->_identity = new UserIdentity($this->username, $this->password);

    if (!$this->_identity->authenticate()) {

      $this->addError('password', 'Неверные имя пользователя или пароль');

    }

  }


  public function login() {

    if ($this->_identity === null) {

      $this->_identity = new UserIdentity($this->username, $this->password);

      $this->_identity->authenticate();

    }


    if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) {

      Yii::app()->user->login($this->_identity);

      return true;

    } else {

      return false;

    }

  }


} 

View: login


<div class="column-left">


  <h2><?php echo Yii::t('login', 'Авторизация'); ?></h2>

  <p id="page-intro"><?php echo Yii::t('login', 'Войдите, чтобы начать работу с TajBay.'); ?></p>


  <?php $form = $this->beginWidget('CActiveForm', array(

    'id' => 'login-form',

    'enableAjaxValidation' => true,

    'clientOptions' => array(

      'validateOnSubmit' => true,

      'validateOnChange' => false,

    ),

  )); ?>


    <div class="content-box">

      <div class="content-box-header">

        <h3><?php echo Yii::t('login', 'Введите имя пользователя и пароль'); ?></h3>


        <div class="clear"></div>

      </div>


      <div class="content-box-content">

        <p>

          <?php echo $form->label($model, 'username', array('label' => Yii::t('register', 'Имя пользователя'))); ?>

          <?php echo $form->textField($model, 'username', array('class' => 'text-input medium-input')); ?>

        </p>


        <p>

          <?php echo $form->label($model, 'password', array('label' => Yii::t('register', 'Пароль'))); ?>

          <?php echo $form->passwordField($model, 'password', array('class' => 'text-input medium-input')); ?>

        </p>


        <p>

          <?php echo $form->checkBox($model, 'remember_me'); ?>

          <?php echo $form->label($model, 'remember_me', array('label' => Yii::t('login', 'Запомнить меня'), 'class' => 'checkbox-label')); ?>

        </p>


        <p>

          <?php echo CHtml::submitButton(Yii::t('login', 'Войти'), array('class' => 'button')); ?>

        </p>

      </div>

    </div>


  <?php $this->endWidget(); ?>


</div>


<div class="column-right">

  <img alt="ad" src="<?php echo Yii::app()->request->baseUrl; ?>/design/images/ad.jpg">

</div>

What’s wrong?

In your view, call $form->error() for each input to receive ajax validation. E.g.

[html]<p>

<?php echo $form->label($model, ‘username’, array(‘label’ => Yii::t(‘register’, ‘Имя пользователя’))); ?>

<?php echo $form->textField($model, ‘username’, array(‘class’ => ‘text-input medium-input’)); ?>

<?php echo $form->error($model,‘username’); ?>

</p>[/html]

Added error messages to the view. After that CDATA began generating. But anyway ajax doesn’t work.


<?php $this->pageTitle = Yii::t('main', 'Вход') . ' - ' . Yii::app()->name; ?>


<div class="column-left">


  <h2><?php echo Yii::t('login', 'Авторизация'); ?></h2>

  <p id="page-intro"><?php echo Yii::t('login', 'Войдите, чтобы начать работу с TajBay.'); ?></p>


  <?php $form = $this->beginWidget('CActiveForm', array(

    'id' => 'login-form',

    'enableAjaxValidation' => true,

    'clientOptions' => array(

      'validateOnSubmit' => true,

      'validateOnChange' => true,

    ),

  )); ?>


    <div class="content-box">

      <div class="content-box-header">

        <h3><?php echo Yii::t('login', 'Введите имя пользователя и пароль'); ?></h3>


        <div class="clear"></div>

      </div>


      <div class="content-box-content">

        <p>

          <?php echo $form->label($model, 'username', array('label' => Yii::t('register', 'Имя пользователя'))); ?>

          <?php echo $form->textField($model, 'username', array('class' => 'text-input medium-input')); ?>

          <?php echo $form->error($model, 'username'); ?>

        </p>


        <p>

          <?php echo $form->label($model, 'password', array('label' => Yii::t('register', 'Пароль'))); ?>

          <?php echo $form->passwordField($model, 'password', array('class' => 'text-input medium-input')); ?>

          <?php echo $form->error($model, 'password'); ?>

        </p>


        <p>

          <?php echo $form->checkBox($model, 'remember_me'); ?>

          <?php echo $form->label($model, 'remember_me', array('label' => Yii::t('login', 'Запомнить меня'), 'class' => 'checkbox-label')); ?>

          <?php echo $form->error($model, 'remember_me'); ?>

        </p>


        <p>

          <?php echo CHtml::submitButton(Yii::t('login', 'Войти'), array('class' => 'button')); ?>

        </p>

      </div>

    </div>


  <?php $this->endWidget(); ?>


</div>


<div class="column-right">

  <img alt="ad" src="<?php echo Yii::app()->request->baseUrl; ?>/design/images/ad.jpg">

</div>

And what happens, when click on submit?

@rudenich

page reloaded with validation errors.

@all

thanx to all, problem was in adding jquery.js script in layout file so firebug was showing two registered jquery.js scripts. one is in the yii core, another in my js folder. solved it by replacing jquery include string with:

<?php Yii::app()->clientScript->registerCoreScript(‘jquery’); ?>