title: yii validation for ajax form post
body: <p>I'm trying to make an ajax calculator using Yii. i know i can use javascript, but since i'm new to Yii i would like to understand how things work.</p>
  
  <p>here is my code in my view</p>
  
  <pre><code>$form = $this-&gt;beginWidget('CActiveForm', array(
          'id'=&gt;'loan-calculator-form', 
          'enableAjaxValidation'=&gt;false, // thisturns off AJAX validation 
          'enableClientValidation'=&gt;true,
          'clientOptions'=&gt;array( 
                  'validateOnSubmit'=&gt;true,
                  'validateOnChange'=&gt;true, 
          ), 
  )); ?&gt;
  
  
  &lt;div class="row"&gt;
      &lt;?php echo $form-&gt;errorSummary($model); ?&gt;
      &lt;div class="col-md-6"&gt;
          &lt;div class="form-body"&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo $form-&gt;labelEx($model,'price'); ?&gt;
                  &lt;div class="input-group input-group-lg"&gt;
                      &lt;span class="input-group-addon"&gt;RM&lt;/span&gt;
                      &lt;?php echo $form-&gt;textField($model,'price',array('class' =&gt; 'form-control')); ?&gt;
                  &lt;/div&gt;
                  &lt;?php echo $form-&gt;error($model,'price'); ?&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo $form-&gt;labelEx($model,'downpayment'); ?&gt;
                  &lt;div class="input-group input-group-lg"&gt;
                      &lt;span class="input-group-addon"&gt;RM&lt;/span&gt;
                      &lt;?php echo $form-&gt;textField($model,'downpayment',array('class' =&gt; 'form-control')); ?&gt;
                  &lt;/div&gt;
                  &lt;?php echo $form-&gt;error($model,'downpayment'); ?&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;?php echo $form-&gt;labelEx($model,'rate'); ?&gt;
                  &lt;div class="input-group input-group-lg"&gt;
                      &lt;?php echo $form-&gt;textField($model,'rate',array('class' =&gt; 'form-control')); ?&gt;
  
                      &lt;span class="input-group-addon"&gt;%&lt;/span&gt;
                  &lt;/div&gt;
                  &lt;?php echo $form-&gt;error($model,'rate'); ?&gt;
              &lt;/div&gt;
  
          &lt;/div&gt;
          &lt;?php echo $form-&gt;error($model,'yearloan'); ?&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="form-footer"&gt;
  
  
      &lt;?php echo CHtml::submitButton("Calculate", 
              array('buttonType'=&gt;'submit', 'class' =&gt; 'btn btn-lg btn-warning btn-block',
                                                                   'onclick'=&gt;"calcLoan();"
                                                                   )
      );
      ?&gt;
  
      &lt;script type="text/javascript"&gt;
          function calcLoan()
          {
              &lt;?php echo CHtml::ajax(array(
                  'url'=&gt;array('/loan-calculator'),
                  'data'=&gt; "js:$(this).serialize()",
                  'type'=&gt;'post',
                  'dataType'=&gt;'json',
                  'cache' =&gt; true, 
                  'success'=&gt;"function(data)
                      {
                          $('#totalLoanAmount').html(data.totalLoanAmount);
                          $('#monthlyInstallment').html(data.monthlyInstallment);
                          $('#loanCalcTable').html(data.loanCalcTable);
                      } ",
              ));
              ?&gt;
              return false; 
  
          }
      &lt;/script&gt;
  </code></pre>
  
  <p>in my model i did add the proper rules like "required"</p>
  
  <p>but when i press submit on my form, the error client side error checking works. if form is valid, the form will post and the onlick will execute too. i do not want the form to post. i want to use an ajax post. </p>
  
  <p>when i set <code>enableAjaxValidation'=&gt;true, 'enableClientValidation'=&gt;true, without the 'clientOptions' array</code> and i submit the form. the ajax post still happens when there are errors. and it returns this json</p>
  
  <p><code>{"CalculatorsForm_price":["Car Purchase Price cannot be blank."],"CalculatorsForm_downpayment":["Down Payment cannot be blank."],"CalculatorsForm_rate":["Interest Rate cannot be blank."]}</code></p>
  
  <p>is there away i do an ajax post ONLY if the form is valid? </p>