Yii ajaxSubmitButton() with fields validation

Viewed 27627

I'm using Yii ajaxSubmitButton() to submit a form. Besides, I have set the 'enableAjaxValidation' parameter to true to validate the corresponding textboxes.

What I am able to do:

  1. Validate the field when the focus leaves it, asynchronously.
  2. Invoke the server side method when the button is clicked, asynchronously.

The problem is that I don't know how to perform the fields validation when the submit button is clicked and, if the model is validated, perform a partial rendering in client side.

If I override the 'success' event in ajaxSubmitButton, I get the partial rendering, but I can't maintain the model validation..

Any help?


EDIT

Thanks for the reply,

The validateOnSubmit flag is already set and the model would be validated correctly if the 'success' event was not set.

When the ajaxSubmitButton is like this:

<?php echo CHtml::ajaxSubmitButton( 'Send',
                                        CHtml::normalizeUrl(array('site/ajaxIndexSubmit')),
                                        array(
                                        'error'=>'js:function(){
                                            alert(\'error\');
                                        }',
                                        'beforeSend'=>'js:function(){
                                            alert(\'beforeSend\');
                                        }',
                                        'success'=>'js:function(data){
                                            alert(\'success, data from server: \'+data);
                                        }',
                                        'complete'=>'js:function(){
                                            alert(\'complete\');
                                        }',
                                        //'update'=>'#where_to_put_the_response',
                                        )
                                    );
    ?>

the alert('success') will print the string corresponding to the model validation. Once I have that string, what logic must be invoken in client side?

The reason to override the 'success' javascript handler is to receive a partial rendering from the server, different to the model validation. I want both things: validation and partial rendering.

6 Answers

I'm about to try sending in a different value (posted with $_GET) on ajaxSubmitButton click. Testing for the value and then running validation if the value doesn't exist. Would that work for you?

Related