From 79514126010988e47bc7ea9d62a4fd0bb38904f3 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Fri, 31 Jul 2020 16:00:20 +0200 Subject: [PATCH] Starting test for validating the whole form via js. --- configure.ac | 3 +- src/cgi.js | 29 ++++++++++++- src/cgi_ini_b4.c | 5 ++- src/cgi_validate.c | 106 +++++++++++++++++++++++++++++++++++++++------ 4 files changed, 126 insertions(+), 17 deletions(-) diff --git a/configure.ac b/configure.ac index e8cbd14..1ad9da0 100644 --- a/configure.ac +++ b/configure.ac @@ -38,7 +38,8 @@ PKG_CHECK_MODULES(ZAKFORMTESTS, [libzakform libzakformgdaex libzakformini libzakcgi - libzakformcgi]) + libzakformcgi + libzakutilsjsonxml]) AC_SUBST(ZAKFORMTESTS_CFLAGS) AC_SUBST(ZAKFORMTESTS_LIBS) diff --git a/src/cgi.js b/src/cgi.js index 7b0faec..9c023ca 100644 --- a/src/cgi.js +++ b/src/cgi.js @@ -9,8 +9,9 @@ $(document).ready (function (){ type: 'get', success: function (data) { alert (data + element.attr('name')); + element.removeClass("is-invalid"); $("#spanMsg_" + element.attr('name')).remove(); - if (data != '') + if (data != '\n') { element.addClass("is-invalid"); element.parent().append("" + data + ""); @@ -18,6 +19,12 @@ $(document).ready (function (){ } }); }); + + $('#myForm').on('submit', function () { + alert("submit " + $(this).attr('name')); + + $("#birthday").remove(); + }); }); function click_add () @@ -36,3 +43,23 @@ function click_remove () $("#name").removeClass("is-invalid"); $("#spanName").remove(); } + +function click_validate_form () +{ + alert ("validate form"); + + $.ajax ({ + url: '/zakformtests/cgi_validate', + //url: '/cgi-bin/env', + method: 'post', + data: $("#name").serialize(), + dataType: 'json', + cache: false, + success: function (data) { + alert ('success ' + data); + }, + 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 7d177ce..484f80b 100644 --- a/src/cgi_ini_b4.c +++ b/src/cgi_ini_b4.c @@ -59,7 +59,8 @@ main (int argc, char *argv[]) "\n" "
\n" "\n" - "\n"); + "\n" + "\n"); val = zak_cgi_main_get_parameter (zakcgimain, "filename"); if (val != NULL) @@ -84,6 +85,8 @@ main (int argc, char *argv[]) form = zak_form_cgi_form_new (zakcgimain, "method", "post", "action", g_strdup_printf ("cgi_ini_b4?filename=%s", filename), + "name", "myForm", + "id", "myForm", NULL); if (zak_form_form_load_from_file (ZAK_FORM_FORM (form), XMLDIR "/cgi.xml")) diff --git a/src/cgi_validate.c b/src/cgi_validate.c index b5a1344..1c52e9a 100644 --- a/src/cgi_validate.c +++ b/src/cgi_validate.c @@ -27,12 +27,15 @@ #include #include #include +#include int main (int argc, char *argv[]) { ZakCgiMain *zakcgimain; + GString *header; + GString *str; GValue *val; @@ -43,9 +46,12 @@ main (int argc, char *argv[]) ZakFormElement *element; + header = NULL; + setlocale (LC_ALL, ""); zakcgimain = zak_cgi_main_new (); + zak_cgi_main_get_stdin (zakcgimain); str = g_string_new (""); @@ -58,8 +64,6 @@ main (int argc, char *argv[]) else { g_warning ("RET no field"); - zak_cgi_main_out (NULL, str->str); - return 0; } val = zak_cgi_main_get_parameter (zakcgimain, "v"); @@ -71,6 +75,13 @@ main (int argc, char *argv[]) else { g_warning ("RET no value"); + } + + if (zak_cgi_main_is_get (zakcgimain) + && (field == NULL + || value == NULL) + ) + { zak_cgi_main_out (NULL, str->str); return 0; } @@ -80,30 +91,97 @@ main (int argc, char *argv[]) if (zak_form_form_load_from_file (ZAK_FORM_FORM (form), XMLDIR "/cgi.xml")) { - element = zak_form_form_get_element_by_id (ZAK_FORM_FORM (form), field); - if (element != NULL) + if (zak_cgi_main_is_get (zakcgimain)) { - zak_form_cgi_form_element_bind (ZAK_FORM_CGI_FORM_ELEMENT (element), zak_utils_gvalue_new_string (value)); - if (!zak_form_element_is_valid (element)) + /* validate single element of the form */ + + element = zak_form_form_get_element_by_id (ZAK_FORM_FORM (form), field); + if (element != NULL) { - g_warning ("RET not valid"); + zak_form_cgi_form_element_bind (ZAK_FORM_CGI_FORM_ELEMENT (element), zak_utils_gvalue_new_string (value)); + if (!zak_form_element_is_valid (element)) + { + g_warning ("RET not valid"); - guint i; - GPtrArray *messages = zak_form_element_get_messages (element); + guint i; + GPtrArray *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)); + for (i = 0; i < messages->len; i++) + { + g_string_append_printf (str, "%s
\n", (gchar *)g_ptr_array_index (messages, i)); + } } } + else + { + g_string_append_printf (str, "Element «%s» not found", field); + } } else { - g_string_append_printf (str, "Element «%s» not found", field); + /* validate the form */ + GPtrArray *ar_elements; + GPtrArray *messages; + guint i; + + JsonBuilder *builder; + JsonGenerator *generator; + + zak_form_cgi_form_bind (form); + + builder = json_builder_new (); + json_builder_begin_array (builder); + + ar_elements = zak_form_form_get_elements (ZAK_FORM_FORM (form)); + for (i = 0; i < ar_elements->len; i++) + { + 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)); + 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); + + generator = json_generator_new (); + json_generator_set_root (generator, json_builder_get_root (builder)); + + g_string_assign (str, json_generator_to_data (generator, NULL)); + + g_object_unref (builder); + g_object_unref (generator); + + /* g_ptr_array_set_size (priv->ar_messages, 0); */ + /* for (i = 0; i < priv->ar_validators->len; i++) */ + /* { */ + /* ZakFormValidator *validator = (ZakFormValidator *)g_ptr_array_index (priv->ar_validators, i); */ + /* if (!zak_form_validator_validate (validator)) */ + /* { */ + /* g_ptr_array_add (priv->ar_messages, (gpointer)g_strdup (zak_form_validator_get_message (validator))); */ + /* ret = FALSE; */ + /* } */ + /* } */ + + header = g_string_new ("Content-Type: application/json"); } } g_warning("RET %s",str->str); - zak_cgi_main_out (NULL, str->str); + zak_cgi_main_out (header->str, str->str); + + g_string_free (header, TRUE); + g_string_free (str, TRUE); return 0; } -- 2.49.0