]> saetta.ns0.it Git - zakform/tests/commitdiff
Starting test for validating the whole form via js.
authorAndrea Zagli <azagli@libero.it>
Fri, 31 Jul 2020 14:00:20 +0000 (16:00 +0200)
committerAndrea Zagli <azagli@libero.it>
Fri, 31 Jul 2020 14:00:20 +0000 (16:00 +0200)
configure.ac
src/cgi.js
src/cgi_ini_b4.c
src/cgi_validate.c

index e8cbd143a0b7bf794cdd1121095b55410907a47c..1ad9da00fdeca14979115abc1b4038e1f8b244ae 100644 (file)
@@ -38,7 +38,8 @@ PKG_CHECK_MODULES(ZAKFORMTESTS, [libzakform
                                  libzakformgdaex
                                  libzakformini
                                  libzakcgi
-                                 libzakformcgi])
+                                 libzakformcgi
+                                 libzakutilsjsonxml])
 
 AC_SUBST(ZAKFORMTESTS_CFLAGS)
 AC_SUBST(ZAKFORMTESTS_LIBS)
index 7b0faec962264c9da27fade11fa077b65e321d37..9c023cac73cfb1d3b1a6322fbbc7d65b7f83e26d 100644 (file)
@@ -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("<span id=\"spanMsg_" + element.attr('name') + "\" class=\"invalid-feedback\">" + data + "</span>");
@@ -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);
+               }
+       });
+}
index 7d177ce2d3312413c3aacd39c6f1a44e835e2886..484f80bc3d6033395fe05f2aad5b14917a02f894 100644 (file)
@@ -59,7 +59,8 @@ main (int argc, char *argv[])
                            "<body>\n"
                            "<div class=\"container\">\n"
                            "<button type=\"button\" class=\"btn btn-primary\" onclick=\"click_add();\">Add</button>\n"
-                           "<button type=\"button\" class=\"btn btn-primary\" onclick=\"click_remove();\">Remove</button>\n");
+                           "<button type=\"button\" class=\"btn btn-primary\" onclick=\"click_remove();\">Remove</button>\n"
+                           "<button type=\"button\" class=\"btn btn-primary\" onclick=\"click_validate_form();\">Validate form</button>\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"))
index b5a13441c4804d1d048b2b0486c53fa16d5163ee..1c52e9a4f54823a6701ff3056b1186039af7e037 100644 (file)
 #include <libzakform/libzakform.h>
 #include <libzakformcgi/libzakformcgi.h>
 #include <libzakutils/libzakutils.h>
+#include <libzakutilsjsonxml/libzakutilsjsonxml.h>
 
 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<br/>\n", (gchar *)g_ptr_array_index (messages, i));
+                                                                       for (i = 0; i < messages->len; i++)
+                                                                               {
+                                                                                       g_string_append_printf (str, "%s<br/>\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<br/>\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;
 }