struts2 - callback method in jquery validation remote to get valid parameter -
struts2 - callback method in jquery validation remote to get valid parameter -
i using jquery validation validate form code -
$('form#register').validate({ rules:{ email:{ required:true, email:true, remote:{ url:"checkemail", type:"post", data:{ email:function(){ homecoming $('input#email').val(); } } } },captcha:{ required:true, remote:{ url:"checkcaptcha", type:"post", data:{ captcha:function(){ homecoming $('input#captcha').val(); } },success:function(data){ if(data.validcaptcha){ homecoming true; }else{ homecoming false; } } } } } });
i checking if mail service regisgtered application or not action checkemail. , validating captcha action checkcaptcha. both of url hitting same action. have next private field.
public class baseaction { private boolean validmail; private boolean validcaptcha; private string email; private string captcha; --getters , setters public string checkmail(){ userservice uservice = new userserviceimpl(); if(uservice.getuserbyemailid(getemail())!=null){ setvalidmail(false); }else{ setvalidmail(true); } homecoming success; } public string checkcaptcha(){ httpservletrequest request = servletactioncontext.getrequest(); string captchastored = (string)request.getsession().getattribute("captcha"); logger.info(captchastored+" = "+getcaptcha()); if(captchastored!=null&&captchastored.equals(getcaptcha())){ setvalidmail(true); }else{ setvalidmail(false); } homecoming success; } }
the problem jquery validation expect value true or false in response action gives response -
{"captcha":null,"email":"admin@ba.co","validcaptcha":false,"validmail":true}
i think it's struts action's default behaviour homecoming value of private property.
now question -
is there way tell struts action want value of valid captcha or validmail true or false. or
is there callback function in jquery validation can farther process response info , can check mail service or captcha validity myself , tell validator it's valid or invalid.
do not client-side...
success: function(data){ if(data.validcaptcha){ homecoming true; }else{ homecoming false; } }
... because that's handled internally plugin.
you're supposed output "false"
(also undefined
, null
, or string) invalid or "true"
valid server-side script , remote
capture & handle response automatically.
documentation:
the serverside resource called via jquery.ajax (xmlhttprequest) , gets key/value pair corresponding name of validated element , value parameter. response evaluated json , must true
valid elements, , can false
, undefined
or null
invalid elements, using default message; or string, eg. "that name taken, seek peter123 instead" display error message.
you not need data
parameter...
data:{ captcha:function(){ homecoming $('input#captcha').val(); } }
... because value of #captcha
field beingness sent server-side script remote
. data
parameter used if need send additional info along request. please refer this illustration in docs.
this should need...
captcha: { required: true, remote: { url: "checkcaptcha", type: "post" } }
quote op:
"is there callback function in jquery validation can farther process response info , can check mail service or captcha validity myself , tell validator it's valid or invalid."
there's no reason that.
if response server true
, you're telling jquery validate field "valid".
if response server false
, null
, undefined
or string, you're telling jquery validate field "invalid". if homecoming string, must json formatted , become error message.
you need prepare server-side code conforms requirement. otherwise, cannot utilize jquery validate plugin.
quote op:
"is there way tell struts action want value of valid captcha or validmail true or false."
i'm no struts expert, instead of return success
, have tried return true
, return false
in server side code? logic in struts action , homecoming appropriate boolean.
jquery struts2 jquery-validate
Comments
Post a Comment