From 4dc999d2fc943bcb671dc843e29a4feca972eae0 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Mon, 20 Jul 2020 10:52:50 +0200 Subject: [PATCH] Real time validation on lost focus via js. --- .gitignore | 4 +- src/Makefile.am | 1 + src/cgi.js | 22 +++++++++ src/cgi_ini_b4.c | 4 +- src/cgi_validate.c | 109 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 3 deletions(-) create mode 100644 src/cgi_validate.c diff --git a/.gitignore b/.gitignore index 17b9837..63be726 100644 --- a/.gitignore +++ b/.gitignore @@ -50,7 +50,9 @@ intltool-* Rules-quot *.exe *.csv -cgi_ini* +cgi_ini +cgi_ini_b4 +cgi_validate cgi_render_each_element get_elements get_element_filters diff --git a/src/Makefile.am b/src/Makefile.am index 7abcd18..e831c29 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,6 +11,7 @@ noinst_PROGRAMS = \ cgi_ini \ cgi_ini_b4 \ cgi_render_each_element \ + cgi_validate \ get_elements \ get_element_filters \ get_element_validators \ diff --git a/src/cgi.js b/src/cgi.js index 970dd3c..7b0faec 100644 --- a/src/cgi.js +++ b/src/cgi.js @@ -1,3 +1,25 @@ +$(document).ready (function (){ + $("#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')); + $("#spanMsg_" + element.attr('name')).remove(); + if (data != '') + { + element.addClass("is-invalid"); + element.parent().append("" + data + ""); + } + } + }); + }); +}); + function click_add () { alert ("add"); diff --git a/src/cgi_ini_b4.c b/src/cgi_ini_b4.c index ff8fd71..7d177ce 100644 --- a/src/cgi_ini_b4.c +++ b/src/cgi_ini_b4.c @@ -148,10 +148,10 @@ main (int argc, char *argv[]) g_string_append (str, "\n\n" - "\n" + "\n" "\n" "\n" - "\n" + "\n" "\n" ""); diff --git a/src/cgi_validate.c b/src/cgi_validate.c new file mode 100644 index 0000000..b5a1344 --- /dev/null +++ b/src/cgi_validate.c @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2016-2020 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include +#include + +#include +#include +#include +#include + +int +main (int argc, char *argv[]) +{ + ZakCgiMain *zakcgimain; + + GString *str; + + GValue *val; + gchar *field; + gchar *value; + + ZakFormCgiForm *form; + + ZakFormElement *element; + + setlocale (LC_ALL, ""); + + zakcgimain = zak_cgi_main_new (); + + str = g_string_new (""); + + val = zak_cgi_main_get_parameter (zakcgimain, "f"); + if (val != NULL) + { + field = (gchar *)g_value_get_string (val); + g_warning ("RET field %s", field); + } + else + { + g_warning ("RET no field"); + zak_cgi_main_out (NULL, str->str); + return 0; + } + + val = zak_cgi_main_get_parameter (zakcgimain, "v"); + if (val != NULL) + { + value = (gchar *)g_value_get_string (val); + g_warning ("RET value %s", value); + } + else + { + g_warning ("RET no value"); + zak_cgi_main_out (NULL, str->str); + return 0; + } + + form = zak_form_cgi_form_new (zakcgimain, + NULL); + + 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) + { + 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); + + 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); + } + } + g_warning("RET %s",str->str); + zak_cgi_main_out (NULL, str->str); + + return 0; +} -- 2.49.0