Custom Validation before ajax submit

Hii All…

I am quite new to YII . I wanted to submit a form via ajax if the form passes custom validation . I tried using ajaxsubmitButton with onclick but it didn’t help . I may be missing a very obvious point here but I have already spent a lot of time finding a solution to this problem. And if there is any other feasible way to accomplish this that will be appreciated

The code snippet I am using is

<?php

echo CHtml::ajaxSubmitButton(Yii::t(‘submit_log’,‘Finish’),CHtml::normalizeUrl(array(‘user/managelogs’,‘render’=>false)),array(‘success’ =>‘function(data){alert(data);}’),array(‘onclick’=>‘return validatealarmlog();’));

and the javascript function is

function validatealarmlog(){

    var flag = true;


    var engine1 = &#036;(&quot;#AlarmLog_strEngineDetails1&quot;).val();    


    var engine2 = &#036;(&quot;#AlarmLog_strEngineDetails2&quot;).val();


    var wagon1 = &#036;(&quot;#AlarmLog_strWagonDetails1&quot;).val();


    var wagon2 = &#036;(&quot;#AlarmLog_strWagonDetails2&quot;).val();


    var wagon3 = &#036;(&quot;#AlarmLog_strWagonDetails3&quot;).val();


    var wagon4 = &#036;(&quot;#AlarmLog_strWagonDetails4&quot;).val();


    var brake1 = &#036;(&quot;#AlarmLog_strBrakeVanDetails1&quot;).val();


    var brake2 = &#036;(&quot;#AlarmLog_strBrakeVanDetails2&quot;).val();


    if((engine1 ==&quot;&quot;) ||(engine2 ==&quot;&quot;) ||(wagon1 ==&quot;&quot;) ||(wagon2==&quot;&quot;)||(wagon3==&quot;&quot;)||(wagon4==&quot;&quot;) ||(brake1==&quot;&quot;)||(brake2==&quot;&quot;)){


        alert(&quot;Enter all DEFECTIVE STOCK DETAILS&quot;);


        //return false;


        flag =false;


    }       


    if(&#33;checkSpecialchar(engine1)){


        //return false;


        flag =false;


    }


   


    if(&#33;checkSpecialchar(engine2)){


        //return false;


        flag =false;


    }


    if(&#33;checkSpecialchar(wagon1)){


        //return false;


        flag =false;


    }


    if(&#33;checkSpecialchar(wagon2)){


        //return false;


        flag =false;


    }


    if(&#33;checkSpecialchar(wagon3)){


        //return false;


        flag =false;


    }


    if(&#33;checkSpecialchar(wagon4)){


        //return false;


        flag =false;


    }


    if(&#33;checkSpecialchar(brake1)){


        //return false;


        flag =false;


    }


    if(&#33;checkSpecialchar(brake2)){


        //return false;


        flag =false;


    }


    var checkFlatcheckbox =&#036;(&quot;#flat_place&quot;).attr('checked');


    if(checkFlatcheckbox == true){


        var length = &#036;(&quot;#length&quot;).val();


        var width = &#036;(&quot;#width&quot;).val();


        var depth = &#036;(&quot;#depth&quot;).val();


        if(length == &quot;&quot; ||width ==&quot;&quot; ||depth ==&quot;&quot;){


            alert(&quot;enter diagnosis flate plate values&quot;);


            //return false;


            flag =false;


        }else{


            if(&#33;checkSpecialchar(length)){


                //return false;


                flag =false;


            }


            if(&#33;checkSpecialchar(width)){


                //return false;


                flag =false;


            }


            if(&#33;checkSpecialchar(depth)){


                //return false;


                flag =false;


            }


        }


    }


   


    var othersCheckbox = &#036;(&quot;#others_check&quot;).attr('checked');


    if(othersCheckbox == true){


        var others = &#036;(&quot;#others&quot;).val();


        if(others == &quot;&quot;){


            alert(&quot;Enter diagnosis other details&quot;);


            //return false;


            flag =false;


        }else{


            if(&#33;checkSpecialchar(others)){


                //return false;


                flag =false;


            }


        }


    }


   


    var out_of_roundness = &#036;(&quot;#out_of_roundness&quot;).attr('checked');


    var defect_in_suspension = &#036;(&quot;#defect_in_suspension&quot;).attr('checked');


    var misalignment_of_bogie = &#036;(&quot;#misalignment_of_bogie&quot;).attr('checked');


    var uneven_loading = &#036;(&quot;#uneven_loading&quot;).attr('checked');


    var skewness_in_carbody = &#036;(&quot;#skewness_in_carbody&quot;).attr('checked');


     


    if(checkFlatcheckbox&#33;=true &amp;&amp; othersCheckbox&#33;=true &amp;&amp; out_of_roundness&#33;=true &amp;&amp; defect_in_suspension&#33;=true &amp;&amp; misalignment_of_bogie&#33;=true &amp;&amp; uneven_loading &#33;=true &amp;&amp; skewness_in_carbody&#33;=true){


        alert(&quot;Enter diagnosis other details&quot;);


        //return false;


        flag =false;


    }


    var trainName = &#036;(&quot;#train_name&quot;).val();


    if(trainName == &quot;&quot;){


        alert(&quot;Enter train name&quot;);


        //return false;


        flag =false;


    }else{


        if(&#33;checkSpecialchar(trainName)){


            //return false;


            flag =false;


        }


    }





    var depot_zone = &#036;(&quot;#depot_zone&quot;).val();


    var depot_division = &#036;(&quot;#depot_division&quot;).val();


    var depot_depo = &#036;(&quot;#depot_depo&quot;).val();


    var air_brake_cert_no = &#036;(&quot;#air_brake_cert_no&quot;).val();


   


    if(depot_zone == &quot;&quot;){


        alert(&quot;Please enter depot zone details&quot;);


        //return false;


        flag =false;


    }else{


        if(&#33;checkSpecialchar(depot_zone)){


            //return false;


            flag =false;


        }


    }


           


    if(depot_division == &quot;&quot;){


        alert(&quot;Please enter depot zone details&quot;);


        //return false;


        flag =false;


    }else{


        if(&#33;checkSpecialchar(depot_division)){


            //return false;


            flag =false;


        }


    }





    if(depot_depo == &quot;&quot;){


        alert(&quot;Please enter depot zone details&quot;);


        //return false;


        flag =false;


    }else{


        if(&#33;checkSpecialchar(depot_depo)){


            //return false;


            flag =false;


        }


    }





    if(air_brake_cert_no == &quot;&quot;){


        alert(&quot;Please enter depot zone details&quot;);


        //return false;


        flag =false;


    }else{


        if(&#33;checkSpecialchar(air_brake_cert_no)){


            //return false;


            flag =false;


        }


    }


   


    var detached = &#036;(&quot;#action_taken_0&quot;).attr('checked');


    var allowed_to_run = &#036;(&quot;#action_taken_1&quot;).attr('checked');


    var feedback_not_recieved = &#036;(&quot;#action_taken_2&quot;).attr('checked');


    if(detached &#33;=true &amp;&amp; allowed_to_run &#33;=true &amp;&amp; feedback_not_recieved &#33;=true){


        alert('Select Action to be taken');


        //return false;


        flag =false;


    }

return flag;

}

The validations does work but when the function returns true then the page gets navigated to the url specified in the ajaxsubmit as if its not an ajax call…

Please Help…

For ajax-validated form you can use a normal submit button, is not needed any modification to the Gii generated form.

You have to enable ajaxValidation and validateOnSubmit, and you can put the ajax submit function in the afterValidate function of AjaxForm (check documentation for details).

Also take a loot at this post.

Hi zaccaria,

Thnx a lot for paying attention to my problem( Though its not been solved due to my negligence :( ).

I will try to explain again what I want to do.

Is there any way from which I can submit a form via ajax only if itafter when it passes through a javascript function… If the function returns true it should go to the specified url otherwise it should not go to the url specified…

Is there any way out…

Thnx a lot for my help…

The Yii validation is always and only server side: even if you use ajax validation, the form will be submitted to the server that will send back the result of the validation.

If you want to avoid this submission, you have to write all on your own, the framework has no instrument for help you.

If you want to follow the Yii way, you have just to turn on ajax validation and add validateOnSubmit: on sumbit the form will be submitted via ajax, checked and if the data passes the validation the page will be actually submitted, if no the errors will be highligted.