hungsama / struts2-jquery

Automatically exported from code.google.com/p/struts2-jquery
0 stars 0 forks source link

Submit a correct form need two HTTP request. #941

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
(This is for feature requests and bugs in Struts2 jQuery Plugin - for
getting help, please use the User Group.
http://groups.google.com/group/struts2-jquery )

What steps will reproduce the problem?
1.Same as the showcase, Form submission with AJAX Validation.
2.
3.

What is the expected output? What do you see instead?
Expecting: if the form is all correct, only one HTTP request was send.
Now I see: Firstly, sending a request with the option of 
struts.validateOnly:true,etc, and if no error was found, and a request againt 
without those options.

Which struts2 version?
2.3.8

Which struts2-jquery plugin version?
3.5

Please provide any additional information below.

What I want is, adding API of jqueryaction open to the developer.

As mentioned above, struts.validateOnly:true can be easily sloved by adding a 
hidden input HTML tag in the from area. But when the js detected no errors 
returned, it going to execute what the jqueryaction option defined action. I've 
looking in to the source code of jquery.struts2.js (line:526), it do supported 
various actions.

I hope to have this promoted.

Original issue reported on code.google.com by kagi...@gmail.com on 19 Jan 2013 at 2:37

GoogleCodeExporter commented 9 years ago
To be simple, I want to have a customize callback when the ajax form was 
submitted successfully while do not have extra HTTP request.

That is the two operation of validation and submit merge into one.

Original comment by kagi...@gmail.com on 19 Jan 2013 at 2:43

GoogleCodeExporter commented 9 years ago
You don't need to use the built-in clientValidation Feature. You can use your 
own way to validate your form. 

Original comment by johgep on 21 Jan 2013 at 12:50

GoogleCodeExporter commented 9 years ago
Well, that a solution. But why don't integrate into the project? Here is my 
solution currently:

$(function(){
    var sj=jQuery.struts2_jquery,$form=$("#register_form"),params={
        type:"POST",
        data:{
            "struts.enableJSONValidation": true
        },
        url:$form[0].action,
        async:false,
        cache:false,
        complete:function(request, status) {
        //  console.log("@complete");
        //  console.log(request);
        //  console.log(status);
        },
        success:function(data){ //*** my call back
            if(data.code===0){
                alert(data.message);// TODO 
            }else{
                bootstrapValidation($form,data);
            }
        },
        dataType:"json"
    };
    sj.require("js/plugins/jquery.form" + sj.minSuffix + ".js");
    $form.submit(function(){
        $form.ajaxSubmit(params);
        return false;
    });
});

And, I want to make it simple by just writing my call back. And the code I 
think would like:

<j:submit cssClass="btn btn-primary" formIds="register_form" validate="true" 
verifySuccess="customCallback" validateFunction="bootstrapValidation"/>
<script>
function customCallback(data){ //*** my call back
    if(data.code===0){
        alert(data.message);// TODO 
    }else{
        bootstrapValidation($form,data);
    }
}
</script>

Notice: when customCallback is not empty, the struts.validateOnly need to be 
false.

I hope you think about it.

Original comment by kagi...@gmail.com on 21 Jan 2013 at 12:58