@view_config(renderer="templates/form.pt", name="ajaxform_redirect")
@demonstrate("AJAX form submission (redirect on success)")
def ajaxform_redirect(self):
class Mapping(colander.Schema):
name = colander.SchemaNode(
colander.String(), description="Content name"
)
date = colander.SchemaNode(
colander.Date(),
widget=deform.widget.DatePartsWidget(),
description="Content date",
)
class Schema(colander.Schema):
number = colander.SchemaNode(colander.Integer())
mapping = Mapping()
schema = Schema()
options = """
{success:
function (rText, sText, xhr, form) {
var loc = xhr.getResponseHeader('X-Relocate');
if (loc) {
document.location = loc;
};
}
}
"""
def succeed():
location = self.request.resource_url(
self.request.root, "thanks.html", route_name="deformdemo"
)
# To appease jquery 1.6+, we need to return something that smells
# like HTML, or we get a "Node cannot be inserted at the
# specified point in the hierarchy" Javascript error. This didn't
# used to be required under JQuery 1.4.
return Response(
"<div>hurr</div>",
headers=[
("X-Relocate", location),
("Content-Type", "text/html"),
],
)
form = deform.Form(
schema, buttons=("submit",), use_ajax=True, ajax_options=options
)
return self.render_form(form, success=succeed)