Validation in form and submit button....

Dear all.

I am using the following codes in my view.php.


<script>

   $('#submitButton').click(function(){

        // $('#loading').show();

        $.prompt('We are uploading and processing your files, please wait and do not refresh this page……');

        // and when finish, call or reload your page:  $('#loading').hide();  

   });

</script>

So basically, when the user clicks on the submit button.

I will prompt them to wait and do not refresh the page.

However, I found that this prompt will occur when the user clicks on submit button even there are still errors in the form(like captcha is not correct).

I want it shows up only the form is validated and the submit button is clicked…

I tried if $model->validate(){ }

but not work…

Any ideas?

any ideas…?

Thanks

It seems you do Ajax validation, right? So this may help you (put it in the relevant view, or in layouts/main.php if it’s applicable to the whole webapp ):


$script = <<<JS

    var headline = $('#loading');

    $(document).ajaxSend(function() {headline.show()});

    $(document).ajaxStop(function() {headline.hide()});

JS;

Yii::app()->clientScript->registerScript('loading-indicator', $script, CClientScript::POS_READY);

So do I need to write a class called loading? It seems that the indicator will show up whenever I click submit or not…I don’t quite understand it…Do I need to delete my own codes above?

Thanks so much for your help.

Sorry I re-read your post and I think I got it wrong. My answer is for a really different goal.

Try to check whether elements with class "errorMessage" shown or not?

not shown…

It’s so weird…

I just want to show the javascript ONLY after the form is validated…

You have an afterValidate option for Yii js validation plugin


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

    'id'=>'some-id',

    'enableAjaxValidation'=>true,

    'clientOptions'=>array(

        'afterValidate'=>'js:function(form, data, hasError) {

            ... // your js code here

        })'

    ),

));

http://www.yiiframework.com/doc/api/1.1/CActiveForm#clientOptions-detail

ummm…I tried


        'afterValidate'=>'js:function(form, data, hasError) {

            $.prompt("We are uploading and processing your files, please wait and do not refresh this page……")

                       }'

       ),

     )); ?>

But the prompt will not show up regardless there are errors or not…

Have you tried with simpler js code like alert(‘hello’); ?

I’ve already done that in a project. I’ll check as soon as I am at my laptop.

I can certify this works


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

    'id'=>'some-form',

    'enableClientValidation'=>false,

    'enableAjaxValidation'=>true,

    'clientOptions'=>array(

        'validateOnSubmit'=>true,

        'validateOnChange'=>false,

        'beforeValidate'=>"js:function(form) {

            alert('Going to validate. Please click Ok and wait few moments.')

            return true;

        }",

        'afterValidate'=>"js:function(form, data, hasError) {

            if(hasError) {

                alert('We have detected some input errors and has not saved your data. Please click Ok to correct them.');

                return false;

            }

            else {

                if(confirm('We have validated your input and we are ready to save your data. Please click Ok to save or Cancel to return to input.'))

                    return true;

                else

                    return false;

            }

        }",

    )

)); ?>

PS I’ve just written the messages because the webapp wasn’t in English :rolleyes: I hope you get the idea

Thanks so much for your help!

If I use your code… it always gives me "validation code is not correct…"

If I delete before validation in your code …then no alert will show up at all…

Is there any chance I can just keep the after validation…?

I tried several ways but failed…

And it’s weird…even if I open the ajax validation, The file input will not be ajax validation because if I leave it empty, it will refresh the page and tell me not allow empty…not tell me via ajax way.

why "validation code is not correct" always show up if I use after validation?

Any one…?

Thanks!!!