Object20object Validation Plugin Flask
Solution 1:
I doubt that you have a handler set up to handle the [object Object]
route. ;-)
The issue seems to be that $SCRIPT_ROOT
is actually some kind of JavaScript object - make sure that the final URL you pass to getJSON
is a string (that points to the correct endpoint).
After you verify that you are hitting the correct endpoint you will need to make sure that you are returning valid JSON:
from flask import jsonify
# additional code@app.route("/_check_mail")defcheck_mail():
# ... snip ...return jsonify(valid=check)
Solution 2:
I managed to fix it (now for real)
The problem was coming from the .getJSON method. This method is a short method for
$.ajax({dataType:"json",url:url,data:data,success:success});
wich is actually already being used by the remote call of the validation plugin!
So the only thing I actually had to do was:
email: {
required:true,
email:true,
remote: {
url:"_check_mail",
data: {
email:function() {
return$("#email").val();
}
}
}
},
Solution 3:
$SCRIPT_ROOT might be undefined. you have to explicitly define it yourself.
<script type=text/javascript>
$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
</script>
edit :
your code is mysterious. $.getJSON is a shortcut for $.ajax to do only GET request and receive a json response. $.getJSON returns javascript object, not a string. how actually do you send your request to check_mail
?
sample code would be:
$.ajax({
type : 'GET',
data : { email : /* argument to be supplied into `mail` var of flask's `check_email` view */ },
url : $SCRIPT_ROOT + '/_check_email',
})
Solution 4:
Use the messages
option instead of trying to read it back from your remote response.
$(document).ready(function() {
$('#myform').validate({
rules: {
email: {
required: true,
email: true,
remote: $.getJSON($SCRIPT_ROOT + "/_check_mail") // make sure it just returns true or false
}
},
messages:{
email: {
remote: "custom error message"
}
}
});
});
Post a Comment for "Object20object Validation Plugin Flask"