}
}
+/**
+ * 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:
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);
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;
}
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
{