From 97c385be3e44024c032f77f9dbe6fd39a2c998b1 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 1 Aug 2020 09:43:33 +0200 Subject: [PATCH] Validating each element for the form via js. --- src/cgi.js | 76 ++++++++++++++++++++++++++++++++-------------- src/cgi_ini_b4.c | 1 + src/cgi_validate.c | 36 ++++++++++++++-------- 3 files changed, 79 insertions(+), 34 deletions(-) diff --git a/src/cgi.js b/src/cgi.js index 9c023ca..b28f572 100644 --- a/src/cgi.js +++ b/src/cgi.js @@ -1,24 +1,24 @@ $(document).ready (function (){ - $("#birthday").focusout (function (){ - alert("focusout"); + // $("#birthday").focusout (function (){ + // alert("focusout"); - var element = $(this); - $.ajax ({ - url: '/zakformtests/cgi_validate', - data: 'f=' + $(this).attr('name') + '&v=' + $(this).val(), - type: 'get', - success: function (data) { - alert (data + element.attr('name')); - element.removeClass("is-invalid"); - $("#spanMsg_" + element.attr('name')).remove(); - if (data != '\n') - { - element.addClass("is-invalid"); - element.parent().append("" + data + ""); - } - } - }); - }); + // var element = $(this); + // $.ajax ({ + // url: '/zakformtests/cgi_validate', + // data: 'f=' + $(this).attr('name') + '&v=' + $(this).val(), + // type: 'get', + // success: function (data) { + // alert (data + element.attr('name')); + // element.removeClass("is-invalid"); + // $("#spanMsg_" + element.attr('name')).remove(); + // if (data != '\n') + // { + // element.addClass("is-invalid"); + // element.parent().append("" + data + ""); + // } + // } + // }); + // }); $('#myForm').on('submit', function () { alert("submit " + $(this).attr('name')); @@ -27,6 +27,33 @@ $(document).ready (function (){ }); }); +function set_error (value, idx, array) +{ + if (value.element == '') return; + + if (value.messages.length == 0) + { + clear_error (value.element); + } + else + { + if ($('#messages').attr('hidden') !== typeof undefined) + { + $('#messages').removeAttr('hidden'); + $('#messages').html('Form not valid!!!'); + } + + $('#' + value.element).addClass("is-invalid"); + $('#' + value.element).parent().append("" + value.messages[0] + ""); + } +} + +function clear_error (field) +{ + $('#' + field).removeClass("is-invalid"); + $("#spanMsg_" + field).remove(); +} + function click_add () { alert ("add"); @@ -46,17 +73,22 @@ function click_remove () function click_validate_form () { - alert ("validate form"); + //alert ("validate form"); $.ajax ({ url: '/zakformtests/cgi_validate', //url: '/cgi-bin/env', method: 'post', - data: $("#name").serialize(), + data: $('#myForm').serialize(), + //contentType: 'multipart/form-data', dataType: 'json', cache: false, success: function (data) { - alert ('success ' + data); + //alert ('success ' + data); + $('#messages').attr('hidden', ''); + $('#messages').html(''); + + data.forEach(set_error); }, error: function (xhr, status, error) { alert (xhr.status + ' ' + xhr.statusText); diff --git a/src/cgi_ini_b4.c b/src/cgi_ini_b4.c index 484f80b..ef5440c 100644 --- a/src/cgi_ini_b4.c +++ b/src/cgi_ini_b4.c @@ -58,6 +58,7 @@ main (int argc, char *argv[]) "\n" "\n" "
\n" + "\n" "\n" "\n" "\n"); diff --git a/src/cgi_validate.c b/src/cgi_validate.c index 1c52e9a..df60382 100644 --- a/src/cgi_validate.c +++ b/src/cgi_validate.c @@ -123,6 +123,7 @@ main (int argc, char *argv[]) GPtrArray *ar_elements; GPtrArray *messages; guint i; + guint m; JsonBuilder *builder; JsonGenerator *generator; @@ -138,19 +139,30 @@ main (int argc, char *argv[]) ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (ar_elements, i); json_builder_begin_object (builder); - zak_utils_json_set_string (builder, - zak_form_element_get_name (element), - zak_form_element_get_value (element)); + + zak_utils_json_set_string (builder, "element", zak_form_element_get_name (element)); + + json_builder_begin_object (builder); + + json_builder_set_member_name (builder, "messages"); + + json_builder_begin_array (builder); + + if (!zak_form_element_is_valid (element)) + { + + messages = zak_form_element_get_messages (element); + + for (m = 0; m < messages->len; m++) + { + json_builder_add_string_value (builder, (gchar *)g_ptr_array_index (messages, m)); + } + + } + json_builder_end_array (builder); + + json_builder_end_object (builder); json_builder_end_object (builder); - /* /\* if (!zak_form_element_is_valid (element)) *\/ */ - /* /\* { *\/ */ - /* /\* messages = zak_form_element_get_messages (element); *\/ */ - - /* /\* for (i = 0; i < messages->len; i++) *\/ */ - /* /\* { *\/ */ - /* /\* g_string_append_printf (str, "%s
\n", (gchar *)g_ptr_array_index (messages, i)); *\/ */ - /* /\* } *\/ */ - /* /\* } *\/ */ } json_builder_end_array (builder); -- 2.49.0