]> saetta.ns0.it Git - libzakcgi/commitdiff
Added function Form::is_valid.
authorAndrea Zagli <azagli@libero.it>
Sat, 29 Aug 2015 10:36:19 +0000 (12:36 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 29 Aug 2015 10:36:19 +0000 (12:36 +0200)
src/form.c
src/form.h
src/formelement.c
src/formelementsubmit.c
src/formelementtext.c
tests/form.c

index a16e735d3a426ba6c1ed00331974ef014c888e28..b061b03465dee550c53f2dc1d039c18f544cb327 100644 (file)
@@ -209,6 +209,43 @@ zak_cgi_form_bind (ZakCgiForm *zakcgiform)
                }
 }
 
+/**
+ * zak_cgi_form_is_valid:
+ * @zakcgiform:
+ *
+ * Returns:
+ */
+gboolean
+zak_cgi_form_is_valid (ZakCgiForm *zakcgiform)
+{
+       GHashTableIter iter;
+       gpointer key;
+       gpointer value;
+
+       gboolean ret;
+
+       ZakCgiFormPrivate *priv;
+
+       priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+       ret = TRUE;
+
+       g_hash_table_iter_init (&iter, priv->ht_elems);
+       while (g_hash_table_iter_next (&iter, &key, &value))
+               {
+                       if (!g_str_has_prefix ((gchar *)key, "{id_"))
+                               {
+                                       if (!zak_cgi_form_element_is_valid ((ZakCgiFormElement *)value))
+                                               {
+                                                       ret = FALSE;
+                                                       break;
+                                               };
+                               }
+               }
+
+       return ret;
+}
+
 /**
  * zak_cgi_form_render_start:
  * @zakcgiform:
index 1525bdec5446c43fddb44fc5d54f1c680673035a..1a6eaa823f86c914e3dcf245ce973398ff80b2ab 100644 (file)
@@ -58,6 +58,7 @@ gboolean zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *el
 gboolean zak_cgi_form_add_str (ZakCgiForm *zakcgiform, const gchar *str);
 
 void zak_cgi_form_bind (ZakCgiForm *zakcgiform);
+gboolean zak_cgi_form_is_valid (ZakCgiForm *zakcgiform);
 
 gchar *zak_cgi_form_render_start (ZakCgiForm *zakcgiform);
 gchar *zak_cgi_form_render (ZakCgiForm *zakcgiform);
index 4042e4e871d6e45839cdccaa7467b401073eb344..060522d9ae6ab364752c150047dd994b99ec39cd 100644 (file)
@@ -96,6 +96,7 @@ zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element)
 
        priv->validation_regex = NULL;
        priv->ht_attrs = NULL;
+       priv->value = NULL;
 }
 
 gchar
index 20db957e971a64bc4f5895814f37967242fe5b44..4888c54381a4b09cddc8818b6bd4bda66eecbc89 100644 (file)
@@ -129,10 +129,7 @@ static gchar
 static gboolean
 zak_cgi_form_element_submit_is_valid (ZakCgiFormElement *element)
 {
-       gboolean ret;
-
-
-       return ret;
+       return TRUE;
 }
 
 /* PRIVATE */
index 1d8b0ee2ff9155592170c1aa0a472b6a2bdcd019..bbdcf66a52353bcd6530ecaa202c586f7b6a7f75 100644 (file)
@@ -115,22 +115,87 @@ static gchar
 
        GHashTable *ht_attrs;
 
+       GValue *value;
+
        ZakCgiFormElementClass *klass;
 
        klass = (ZakCgiFormElementClass *)g_type_class_peek_parent (ZAK_CGI_FORM_ELEMENT_TEXT_GET_CLASS (ZAK_CGI_FORM_ELEMENT_TEXT (element)));
 
        ht_attrs = klass->get_ht_attrs (element);
 
+       value = zak_cgi_form_element_get_value (element);
+       if (value != NULL)
+               {
+                       g_hash_table_insert (ht_attrs, (gpointer)"value", (gpointer)g_value_get_string (value));
+               }
+
        ret = zak_cgi_tag_text_ht (zak_cgi_form_element_get_id (element), ht_attrs);
 
        return ret;
 }
 
+static gboolean
+zak_cgi_form_element_text_check_value (const gchar *validation_regex, GValue *value)
+{
+       gboolean ret;
+
+       GRegex *regex;
+       GError *error;
+
+       if (G_VALUE_HOLDS (value, G_TYPE_STRING))
+               {
+                       error = NULL;
+                       regex = g_regex_new (validation_regex, 0, 0, &error);
+                       if (regex == NULL
+                               || error != NULL)
+                               {
+                                       syslog (LOG_MAKEPRI(LOG_SYSLOG, LOG_DEBUG), "Error on creating regex: %s.",
+                                                       error->message != NULL ? error->message : "no details");
+                                       return FALSE;
+                               }
+
+                       ret = g_regex_match ((const GRegex *)regex, g_value_get_string (value), 0, NULL);
+               }
+       else
+               {
+                       ret = FALSE;
+               }
+
+       return ret;
+}
+
 static gboolean
 zak_cgi_form_element_text_is_valid (ZakCgiFormElement *element)
 {
        gboolean ret;
 
+       GValue *gval;
+
+       gchar *str_regex;
+
+       gval = zak_cgi_form_element_get_value (element);
+
+       g_object_get (G_OBJECT (element),
+                                 "validation-regex", &str_regex,
+                                 NULL);
+
+       if (G_VALUE_HOLDS (gval, G_TYPE_PTR_ARRAY))
+               {
+                       guint i;
+                       GPtrArray *ar = (GPtrArray *)g_value_get_boxed (gval);
+                       for (i = 0; i < ar->len; i++)
+                               {
+                                       if (!zak_cgi_form_element_text_check_value (str_regex, (GValue *)g_ptr_array_index (ar, i)))
+                                               {
+                                                       ret = FALSE;
+                                                       break;
+                                               }
+                               }
+               }
+       else
+               {
+                       ret = zak_cgi_form_element_text_check_value (str_regex, gval);
+               }
 
        return ret;
 }
index aebbd1f245301a85700bb0bc691a962c352a5711..e7d5837290d66018df3b42db56539a6467e264cd 100644 (file)
@@ -53,6 +53,16 @@ main (int argc, char *argv[])
        if (zak_cgi_main_is_post (zakcgimain))
                {
                        /* validating the form */
+                       zak_cgi_form_bind (form);
+                       if (zak_cgi_form_is_valid (form))
+                               {
+                                       g_string_append (str, "Form is valid!!!");
+                               }
+                       else
+                               {
+                                       g_string_append (str, zak_cgi_form_render (form));
+                                       g_string_append (str, "Form is not valid!!!");
+                               }
                }
        else
                {