]> saetta.ns0.it Git - zakform/tests/commitdiff
Validating each element for the form via js.
authorAndrea Zagli <azagli@libero.it>
Sat, 1 Aug 2020 07:43:33 +0000 (09:43 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 1 Aug 2020 07:43:33 +0000 (09:43 +0200)
src/cgi.js
src/cgi_ini_b4.c
src/cgi_validate.c

index 9c023cac73cfb1d3b1a6322fbbc7d65b7f83e26d..b28f572b872966c0dbeacff69c6ccac1faa19f5c 100644 (file)
@@ -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("<span id=\"spanMsg_" + element.attr('name') + "\" class=\"invalid-feedback\">" + data + "</span>");
-                               }
-                       }
-               });
-       });
+       //      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("<span id=\"spanMsg_" + element.attr('name') + "\" class=\"invalid-feedback\">" + data + "</span>");
+       //                      }
+       //              }
+       //      });
+       // });
 
        $('#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("<span id=\"spanMsg_" + value.element + "\" class=\"invalid-feedback\">" + value.messages[0] + "</span>");
+       }
+}
+
+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);
index 484f80bc3d6033395fe05f2aad5b14917a02f894..ef5440cdaa3d70b9af15c6d5519a2af25389a456 100644 (file)
@@ -58,6 +58,7 @@ main (int argc, char *argv[])
                            "</head>\n"
                            "<body>\n"
                            "<div class=\"container\">\n"
+                           "<div id=\"messages\" class=\"alert alert-danger\" role=\"alert\" hidden></div>\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_validate_form();\">Validate form</button>\n");
index 1c52e9a4f54823a6701ff3056b1186039af7e037..df6038264d72c5a425ecf449a823b9337cbf8808 100644 (file)
@@ -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<br/>\n", (gchar *)g_ptr_array_index (messages, i)); *\/ */
-                                       /*              /\*                     } *\/ */
-                                       /*              /\*     } *\/ */
                                                }
 
                                        json_builder_end_array (builder);