Yii Framework Forum: Ajax Validation Only On Submit - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ajax Validation Only On Submit

#1 User is offline   Magister 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 07-November 12

Posted 07 November 2012 - 03:04 PM

I have been working on this problem for the last 2 days and I finally figured it out. I needed AJAX validation only when submitting a form and client side validation on each field change. From what I was able to figure out this combination is not supported by the CActiveForm object.

I wanted to share my solution since searching for this turned up nothing.

My CActiveForm config:
$form=$this->beginWidget ('CActiveForm', array (
 'id' => 'myform',
// 'enableAjaxValidation' => true, // Disabled to prevent ajax calls for every field update
 'enableClientValidation' => true,
 'clientOptions' => array(
  'validateOnSubmit' => true,
  'validateOnChange' => true, // allow client validation for every field

My ajaxSubmitButton config:
CHtml::ajaxSubmitButton ('Save', Yii::app()->request->url, array (
      'dataType' => 'json', // If this option is not set, the returned data is a string not a JSON object
      'success' =>
'js:function (data) {
 if (data) {
  $.each (settings.attributes, function (i) {
    $.fn.yiiactiveform.updateInput (settings.attributes[i], data, form);
 } else {
  alert (\'success\');
), array (
 'id' => 'myform_submit_'.rand(1,255), // Need a unique id or they start to conflict with more than one load.

I hope this helps someone out trying to figured out the same problem.

One thing to note, the $_POST['ajax'] var is not set by this method so you have to use the Yii::app()->request->isAjaxRequest to find out if this is a regular page load or a ajax request.

#2 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,558
  • Joined: 12-October 09
  • Location:Croatia

Posted 08 November 2012 - 04:48 AM

NOTE: moved to proper section (Tips, Snippets and Tutorials instead of General Discussion for Yii 1.1.x)

Nice tip ;)

Magister, welcome to the yii forum
Find more about me.... btw. Do you know your WAN IP?

#3 User is offline   awebdeveloper 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 55
  • Joined: 29-September 12

Posted 12 November 2012 - 12:57 PM

cool .... thanks

#4 User is offline   phreak 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 86
  • Joined: 19-February 09

Posted 16 December 2012 - 07:35 AM

very nice tip. really needed to know how to validate form in JS with yii tools. Just a little remark. if(data) always returns true, so I use


#5 User is offline   clapas 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 39
  • Joined: 21-February 11

Posted 13 February 2013 - 08:16 AM

I ended up with a very similar code. Just to contribute, I will add some more related tips:

  • You could add
    $.fn.yiiactiveform.updateSummary(form, data);
    after the loop where the updateInput calls are issued.

  • I also used
    as phreak suggests.

  • In the controller you want to return: a) an empty object when the save action was successfull; b) the validation errors in the proper format otherwise. Here is my controller's snippet:
    $result = CActiveForm::validate($model);
    if ($result == '[]') $this->saveModel($model);
    echo $result;

Hope it helps. Cheers!

Share this topic:

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users