From: Andrea Zagli Date: Sat, 29 Aug 2015 10:36:19 +0000 (+0200) Subject: Added function Form::is_valid. X-Git-Tag: v0.1.0~3^2~44 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=fa5f213afa4f939048bef0d83976d928dcdb33da;p=libzakcgi Added function Form::is_valid. --- diff --git a/src/form.c b/src/form.c index a16e735..b061b03 100644 --- a/src/form.c +++ b/src/form.c @@ -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: diff --git a/src/form.h b/src/form.h index 1525bde..1a6eaa8 100644 --- a/src/form.h +++ b/src/form.h @@ -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); diff --git a/src/formelement.c b/src/formelement.c index 4042e4e..060522d 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -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 diff --git a/src/formelementsubmit.c b/src/formelementsubmit.c index 20db957..4888c54 100644 --- a/src/formelementsubmit.c +++ b/src/formelementsubmit.c @@ -129,10 +129,7 @@ static gchar static gboolean zak_cgi_form_element_submit_is_valid (ZakCgiFormElement *element) { - gboolean ret; - - - return ret; + return TRUE; } /* PRIVATE */ diff --git a/src/formelementtext.c b/src/formelementtext.c index 1d8b0ee..bbdcf66 100644 --- a/src/formelementtext.c +++ b/src/formelementtext.c @@ -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; } diff --git a/tests/form.c b/tests/form.c index aebbd1f..e7d5837 100644 --- a/tests/form.c +++ b/tests/form.c @@ -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 {