Commit 3f46194b authored by Ivan Tyagov's avatar Ivan Tyagov

Allow gadget preferences form validation to happen at server side and show...

Allow gadget preferences form validation to happen at server side and show message to use to correct input.
parent afa2e1d2
...@@ -53,7 +53,9 @@ ...@@ -53,7 +53,9 @@
<value> <string>"""\n <value> <string>"""\n
This script edits a Knowledge Box instance used for saving a Gadget preferences.\n This script edits a Knowledge Box instance used for saving a Gadget preferences.\n
"""\n """\n
# XXX: How validation\n from Products.Formulator.Errors import FormValidationError\n
from json import dumps\n
\n
kw = {}\n kw = {}\n
request = context.REQUEST\n request = context.REQUEST\n
form = request.form\n form = request.form\n
...@@ -61,6 +63,30 @@ fields = filter(lambda x: x.startswith(form_fields_main_prefix), form.keys())\n ...@@ -61,6 +63,30 @@ fields = filter(lambda x: x.startswith(form_fields_main_prefix), form.keys())\n
box = context.restrictedTraverse(box_relative_url)\n box = context.restrictedTraverse(box_relative_url)\n
portal_selection = context.getPortalObject().portal_selections\n portal_selection = context.getPortalObject().portal_selections\n
\n \n
# do validation\n
form = getattr(context,form_id)\n
try:\n
# Validate\n
form.validate_all_to_request(request, key_prefix=form_fields_main_prefix)\n
except FormValidationError, validation_errors:\n
# Pack errors into the request\n
field_errors = form.ErrorFields(validation_errors)\n
request.set(\'field_errors\', field_errors)\n
# we need form rendered in gadget mode\n
request.set(\'is_gadget_mode\', 1)\n
# Make sure editors are pushed back as values into the REQUEST object\n
for f in form.get_fields():\n
field_id = f.id\n
if request.has_key(field_id):\n
value = request.get(field_id)\n
if callable(value):\n
value(request)\n
# return validation failed code and rendered form\n
result = {\'content\': form(request, key_prefix=form_fields_main_prefix),\n
\'validation_status\': 0}\n
return dumps(result)\n
\n
form = request.form\n
# get interesting for us fields and save\n # get interesting for us fields and save\n
listbox_selection_field_prefix = \'%s_my_listbox_selection_\' %form_fields_main_prefix\n listbox_selection_field_prefix = \'%s_my_listbox_selection_\' %form_fields_main_prefix\n
for field in fields:\n for field in fields:\n
...@@ -74,15 +100,20 @@ for field in fields:\n ...@@ -74,15 +100,20 @@ for field in fields:\n
portal_selection.setSelectionParamsFor(selection_name, params)\n portal_selection.setSelectionParamsFor(selection_name, params)\n
kw[field.replace(\'%s_my_\' %form_fields_main_prefix, \'\')] = form[field]\n kw[field.replace(\'%s_my_\' %form_fields_main_prefix, \'\')] = form[field]\n
\n \n
# edit\n
box.edit(**kw)\n
\n
if not synchronous_mode:\n
# return JSON in asynchronous mode\n
result = {\'content\': \'\',\n
\'validation_status\': 1}\n
return dumps(result)\n
\n
# determine redirect URL as passed from gadget preference form\n # determine redirect URL as passed from gadget preference form\n
if gadget_redirect_url is None:\n if gadget_redirect_url is None:\n
# taking URL1 as the base of the original URL. \n # taking URL1 as the base of the original URL. \n
# it works for both synchronous and asynchronous gadgets\n # it works for both synchronous and asynchronous gadgets\n
gadget_redirect_url = request[\'URL1\']\n gadget_redirect_url = request[\'URL1\']\n
\n
# edit\n
box.edit(**kw)\n
\n
request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n
%(gadget_redirect_url, \n %(gadget_redirect_url, \n
context.Base_translateString(\'Preference updated.\')))\n context.Base_translateString(\'Preference updated.\')))\n
...@@ -90,7 +121,7 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n ...@@ -90,7 +121,7 @@ request.RESPONSE.redirect(\'%s?portal_status_message=%s\'\n
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>form_fields_main_prefix, box_relative_url, gadget_redirect_url=None</string> </value> <value> <string>form_id, form_fields_main_prefix, box_relative_url, gadget_redirect_url=None, synchronous_mode=True</string> </value>
</item> </item>
<item> <item>
<key> <string>id</string> </key> <key> <string>id</string> </key>
......
...@@ -51,10 +51,17 @@ ...@@ -51,10 +51,17 @@
<item> <item>
<key> <string>_body</string> </key> <key> <string>_body</string> </key>
<value> <string>pad_relative_url = context.getRelativeUrl()\n <value> <string>pad_relative_url = context.getRelativeUrl()\n
return \'\'\'onkeypress="submitGadgetPreferenceFormOnEnter(event, \'%s\',\'%s\');"\'\'\' \\\n edit_form_id = context.getSpecialiseValue().getEditFormId()\n
%(\'gadget_preference_%s_field\' %pad_relative_url.replace(\'/\', \'_\'), pad_relative_url)\n context.log(\'%s\' %edit_form_id)\n
\n
return \'\'\'onkeypress="submitGadgetPreferenceFormOnEnter(event, \'%s\',\'%s\', \'%s\');"\'\'\' \\\n
%(\'gadget_preference_%s_field\' %pad_relative_url.replace(\'/\', \'_\'), pad_relative_url, edit_form_id)\n
</string> </value> </string> </value>
</item> </item>
<item>
<key> <string>_params</string> </key>
<value> <string></string> </value>
</item>
<item> <item>
<key> <string>id</string> </key> <key> <string>id</string> </key>
<value> <string>KnowledgeBox_generateSubmitFormJavaScriptCode</string> </value> <value> <string>KnowledgeBox_generateSubmitFormJavaScriptCode</string> </value>
......
...@@ -126,7 +126,8 @@ ...@@ -126,7 +126,8 @@
tal:define="form_fields_main_prefix python: \'gadget_preference_%s_field\' %box_dom_id;">\n tal:define="form_fields_main_prefix python: \'gadget_preference_%s_field\' %box_dom_id;">\n
\n \n
<!-- render edit gadget preferences form -->\n <!-- render edit gadget preferences form -->\n
<span tal:replace="structure python: edit_form_object(key_prefix=form_fields_main_prefix)"/>\n <div class="edit-form-content"\n
tal:content="structure python: edit_form_object(key_prefix=form_fields_main_prefix)"/>\n
\n \n
<tal:block tal:condition="not: is_asynchronous_gadget">\n <tal:block tal:condition="not: is_asynchronous_gadget">\n
\n \n
...@@ -138,7 +139,10 @@ ...@@ -138,7 +139,10 @@
<button type="submit"\n <button type="submit"\n
i18n:translate="" \n i18n:translate="" \n
i18n:domain="ui"\n i18n:domain="ui"\n
tal:attributes="onclick string:submitSynchronousGadgetPreferenceForm(\'${form_fields_main_prefix}\',\'${box_relative_url}\');\n tal:attributes="onclick string:submitSynchronousGadgetPreferenceForm(\n
\'${form_fields_main_prefix}\',\n
\'${box_relative_url}\',\n
\'${edit_form_id}\');\n
id string: submit_button_${box_dom_id};">Save</button>\n id string: submit_button_${box_dom_id};">Save</button>\n
\n \n
</tal:block>\n </tal:block>\n
...@@ -153,7 +157,8 @@ ...@@ -153,7 +157,8 @@
\'${view_form_id}\', \n \'${view_form_id}\', \n
\'${box_relative_url}\', \n \'${box_relative_url}\', \n
\'${view_form_dom_id}\', \n \'${view_form_dom_id}\', \n
\'${form_fields_main_prefix}\');\n \'${form_fields_main_prefix}\',\n
\'${edit_form_id}\');\n
id string:submit_button_${box_dom_id};">Save</button>\n id string:submit_button_${box_dom_id};">Save</button>\n
\n \n
\n \n
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
</item> </item>
<item> <item>
<key> <string>_EtagSupport__etag</string> </key> <key> <string>_EtagSupport__etag</string> </key>
<value> <string>ts00370324.82</string> </value> <value> <string>ts07964628.69</string> </value>
</item> </item>
<item> <item>
<key> <string>__name__</string> </key> <key> <string>__name__</string> </key>
...@@ -175,34 +175,42 @@ function createDefaultPadOnServer(timeout, default_pad_group, return_url, mode){ ...@@ -175,34 +175,42 @@ function createDefaultPadOnServer(timeout, default_pad_group, return_url, mode){
success: function(data){checkForActivitiesOnServer(timeout, return_url, mode, default_pad_group);}});\n success: function(data){checkForActivitiesOnServer(timeout, return_url, mode, default_pad_group);}});\n
}\n }\n
\n \n
function submitGadgetPreferenceFormOnEnter(event, form_fields_main_prefix, box_relative_url){\n function submitGadgetPreferenceFormOnEnter(event, \n
form_fields_main_prefix, \n
box_relative_url, \n
edit_form_id){\n
/* This function can be used to submit gadget preferences form whenever\n /* This function can be used to submit gadget preferences form whenever\n
an enter is pressed in form */\n an enter is pressed in form */\n
if(event.keyCode == 13){submitSynchronousGadgetPreferenceForm(form_fields_main_prefix, box_relative_url);}\n if(event.keyCode == 13){submitSynchronousGadgetPreferenceForm(form_fields_main_prefix, \n
box_relative_url, \n
edit_form_id);}\n
}\n }\n
\n \n
function submitSynchronousGadgetPreferenceForm(\n function submitSynchronousGadgetPreferenceForm(\n
form_fields_main_prefix, \n form_fields_main_prefix, \n
box_relative_url){\n box_relative_url,\n
edit_form_id){\n
/* this will add respective gadget knowledge box relative url and\n /* this will add respective gadget knowledge box relative url and\n
gadget ERP5 preference form field_prefix (so multiple gadgets can \n gadget ERP5 preference form field_prefix (so multiple gadgets can \n
safely coexist in one HTML page with one HTML form */\n safely coexist in one HTML page with one HTML form */\n
redirect_url = window.location.protocol + "//" + window.location.host + window.location.pathname\n redirect_url = window.location.protocol + "//" + window.location.host + window.location.pathname\n
$("form").append(\'<input type="hidden" name="box_relative_url" value="\' +box_relative_url + \'">\');\n $("form").append(\'<input type="hidden" name="box_relative_url" value="\' +box_relative_url + \'">\')\n
$("form").append(\'<input type="hidden" name="form_fields_main_prefix" value="\' +form_fields_main_prefix + \'">\');\n $("form").append(\'<input type="hidden" name="form_fields_main_prefix" value="\' +form_fields_main_prefix + \'">\')\n
$("form").append(\'<input type="hidden" name="gadget_redirect_url" value="\' +redirect_url + \'">\');\n $("form").append(\'<input type="hidden" name="gadget_redirect_url" value="\' +redirect_url + \'">\')\n
clickSaveButton(knowledge_box_edit_script_id); \n $("form").append(\'<input type="hidden" name="form_id" value="\' +edit_form_id + \'">\') \n
};\n clickSaveButton(knowledge_box_edit_script_id)\n
}\n
\n \n
function submitAsynchronousGadgetPreferenceForm(\n function submitAsynchronousGadgetPreferenceForm(\n
form_dom_id, \n form_dom_id, \n
view_form_url, \n view_form_url, \n
box_relative_url, \n box_relative_url, \n
visual_block_dom_id, \n visual_block_dom_id, \n
form_fields_main_prefix){\n form_fields_main_prefix,\n
edit_form_id){\n
/* Iterate over all possible form elements within edit form,\n /* Iterate over all possible form elements within edit form,\n
collect them and send to server*/\n collect them and send to server*/\n
var request_str = "box_relative_url=" + box_relative_url+ "&form_fields_main_prefix=" + form_fields_main_prefix + "&";\n var request_str = "synchronous_mode:int=0&" + "box_relative_url=" + box_relative_url+ "&form_fields_main_prefix=" + form_fields_main_prefix + "&form_id="+edit_form_id + "&";\n
\n \n
//input tags\n //input tags\n
$("#" + form_dom_id).find("input").each(\n $("#" + form_dom_id).find("input").each(\n
...@@ -234,12 +242,23 @@ function submitAsynchronousGadgetPreferenceForm(\n ...@@ -234,12 +242,23 @@ function submitAsynchronousGadgetPreferenceForm(\n
if(option.attr("selected")){request_str+=element.attr("name") + \'=\' + option.val() + \'&\';}\n if(option.attr("selected")){request_str+=element.attr("name") + \'=\' + option.val() + \'&\';}\n
}); }\n }); }\n
else{request_str+=name + \'=\' + value + \'&\';} });\n else{request_str+=name + \'=\' + value + \'&\';} });\n
\n \n
// save form preferences to remote server\n // save form preferences to remote server\n
$.ajax({url: knowledge_box_edit_script_id + "?" + request_str,\n $.ajax({url: knowledge_box_edit_script_id + "?" + request_str,\n
dataType: "json",\n
success: function (data){\n success: function (data){\n
updater(view_form_url, box_relative_url, visual_block_dom_id);\n if (data.validation_status){\n
$("#" + form_dom_id).toggle();} });\n // server side validation passed\n
updater(view_form_url, box_relative_url, visual_block_dom_id)\n
$("#" + form_dom_id).toggle()\n
// clean error messages\n
$("#" + form_dom_id + " span.error").remove()\n
}\n
else{\n
// server side validation failed show error message\n
$("#" + form_dom_id + " div.edit-form-content").html(data.content)\n
}\n
} });\n
}\n }\n
\n \n
function updateServerBoxColumnLayout(event, ui){\n function updateServerBoxColumnLayout(event, ui){\n
...@@ -493,7 +512,7 @@ $(document).ready(initialize);\n ...@@ -493,7 +512,7 @@ $(document).ready(initialize);\n
</item> </item>
<item> <item>
<key> <string>size</string> </key> <key> <string>size</string> </key>
<value> <int>18244</int> </value> <value> <int>19261</int> </value>
</item> </item>
<item> <item>
<key> <string>title</string> </key> <key> <string>title</string> </key>
......
721 722
\ No newline at end of file \ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment