Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Hey I had same problem and dint get it working even with aftervalidate, beforevalidate and so on.And secondly I dint like to use a extension for this coz my app already has many. So I made this: </p> <p>Edit : As per Barth's suggestion , i am putting those code here itself.</p> <p>Step 1: @ your controller action</p> <pre><code>public function actionMyAction() { $model=new User; $this-&gt;performAjaxValidation($model); if(isset($_POST['User'])) { $model-&gt;attributes=$_POST['User']; $valid=$model-&gt;validate(); if($valid){ //do anything here echo CJSON::encode(array( 'status'=&gt;'success' )); Yii::app()-&gt;end(); } else{ $error = CActiveForm::validate($model); if($error!='[]') echo $error; Yii::app()-&gt;end(); } } } </code></pre> <p>Step 2: @ your view Your form may look like this</p> <pre><code>&lt;?php $form=$this-&gt;beginWidget('CActiveForm', array( 'id'=&gt;'user-form', 'enableAjaxValidation'=&gt;true, 'action'=&gt;$this-&gt;createUrl('myController/MyAction'), 'enableClientValidation'=&gt;true, )); ?&gt; &lt;div class="errorMessage" id="formResult"&gt;&lt;/div&gt; &lt;div id="AjaxLoader" style="display: none"&gt;&lt;img src="&lt;?php echo Yii::app()-&gt;request-&gt;baseUrl; ?&gt;/images/spinner.gif"&gt;&lt;/img&gt;&lt;/div&gt; &lt;div class="row-user-single"&gt; &lt;?php echo $form-&gt;labelEx($model,'attribute1'); ?&gt; &lt;?php echo $form-&gt;passwordField($model,'attribute1',array('size'=&gt;60,'maxlength'=&gt;500)); ?&gt; &lt;?php echo $form-&gt;error($model,'attribute1'); ?&gt; &lt;/div&gt; &lt;div class="row-user-single"&gt; &lt;?php echo $form-&gt;labelEx($model,'attribute2'); ?&gt; &lt;?php echo $form-&gt;passwordField($model,'attribute2',array('size'=&gt;60,'maxlength'=&gt;500)); ?&gt; &lt;?php echo $form-&gt;error($model,'attribute2'); ?&gt; &lt;/div&gt; &lt;div class="buttons"&gt; &lt;?php echo CHtml::ajaxSubmitButton('Save',CHtml::normalizeUrl(array('myController/MyAction','render'=&gt;true)), array( 'dataType'=&gt;'json', 'type'=&gt;'post', 'success'=&gt;'function(data) { $("#AjaxLoader").hide(); if(data.status=="success"){ $("#formResult").html("form submitted successfully."); $("#user-form")[0].reset(); } else{ $.each(data, function(key, val) { $("#user-form #"+key+"_em_").text(val); $("#user-form #"+key+"_em_").show(); }); } }', 'beforeSend'=&gt;'function(){ $("#AjaxLoader").show(); }' ),array('id'=&gt;'mybtn','class'=&gt;'class1 class2')); ?&gt; </code></pre> <p>This is working just fine for me And all the code written above are my style of writing. You may change them as required but, there are only two things to be noted as follows: 1. @ your controller call validate of your model and return a json object if form is invalid 2. @ your view break this json object (OR traverse through it) and show the error messages under respective elements of the form.</p> <p>Keep it simple :)</p> <p>You may see my blog post here too: <a href="http://www.yiiframework.com/forum/index.php/topic/37075-form-validation-with-ajaxsubmitbutton/" rel="noreferrer">http://www.yiiframework.com/forum/index.php/topic/37075-form-validation-with-ajaxsubmitbutton/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload