]> saetta.ns0.it Git - libzakcgi/commitdiff
Added function ZakCgiForm::bind.
authorAndrea Zagli <azagli@libero.it>
Sat, 29 Aug 2015 09:46:30 +0000 (11:46 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 29 Aug 2015 09:46:30 +0000 (11:46 +0200)
src/commons.c
src/form.c
src/form.h
src/formelement.c
src/formelement.h
tests/form.c

index 93c5075571b5dc464e15720b7ca6541129d88666..421a5015fb18256c8ede2b1da7692571c9bce905 100644 (file)
@@ -76,7 +76,7 @@ gchar
        while (g_hash_table_iter_next (&iter, &key, &value))
                {
                        g_string_append_printf (str,
-                                                                       " \"%s\"=\"%s\"",
+                                                                       " %s=\"%s\"",
                                                                        (gchar *)key,
                                                                        (gchar *)value);
                }
index da81760728c085ca751b93b11375bcb017d3018e..a16e735d3a426ba6c1ed00331974ef014c888e28 100644 (file)
@@ -175,6 +175,40 @@ zak_cgi_form_add_str (ZakCgiForm *zakcgiform, const gchar *str)
        return ret;
 }
 
+/**
+ * zak_cgi_form_bind:
+ * @zakcgiform:
+ *
+ */
+void
+zak_cgi_form_bind (ZakCgiForm *zakcgiform)
+{
+       GHashTableIter iter;
+       gpointer key;
+       gpointer value;
+
+       GValue *gval;
+
+       ZakCgiFormPrivate *priv;
+
+       g_return_if_fail (ZAK_CGI_IS_FORM (zakcgiform));
+
+       priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+       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_"))
+                               {
+                                       gval = zak_cgi_main_get_stdin_field (priv->zakcgimain, (gchar *)key);
+                                       if (gval != NULL)
+                                               {
+                                                       zak_cgi_form_element_set_value ((ZakCgiFormElement *)value, gval);
+                                               }
+                               }
+               }
+}
+
 /**
  * zak_cgi_form_render_start:
  * @zakcgiform:
@@ -190,6 +224,8 @@ gchar
 
        ZakCgiFormPrivate *priv;
 
+       g_return_if_fail (ZAK_CGI_IS_FORM (zakcgiform));
+
        priv = ZAK_CGI_FORM_GET_PRIVATE (zakcgiform);
 
        str = g_string_new ("<form");
index 366afdb761752a410fb290320249bd3396d7b626..1525bdec5446c43fddb44fc5d54f1c680673035a 100644 (file)
@@ -57,6 +57,8 @@ ZakCgiForm *zak_cgi_form_new (ZakCgiMain *zakcgimain, ...);
 gboolean zak_cgi_form_add_element (ZakCgiForm *zakcgiform, ZakCgiFormElement *element);
 gboolean zak_cgi_form_add_str (ZakCgiForm *zakcgiform, const gchar *str);
 
+void zak_cgi_form_bind (ZakCgiForm *zakcgiform);
+
 gchar *zak_cgi_form_render_start (ZakCgiForm *zakcgiform);
 gchar *zak_cgi_form_render (ZakCgiForm *zakcgiform);
 
index 60cbc840c3521dda3979649f819001a72e32996e..4042e4e871d6e45839cdccaa7467b401073eb344 100644 (file)
@@ -61,6 +61,7 @@ struct _ZakCgiFormElementPrivate
                gchar *id;
                gchar *validation_regex;
                GHashTable *ht_attrs;
+               GValue *value;
        };
 
 G_DEFINE_TYPE (ZakCgiFormElement, zak_cgi_form_element, G_TYPE_OBJECT)
@@ -149,6 +150,37 @@ gchar
        return ret;
 }
 
+/**
+ * zak_cgi_form_element_set_value:
+ * @element:
+ * @value:
+ *
+ */
+void
+zak_cgi_form_element_set_value (ZakCgiFormElement *element, GValue *value)
+{
+       ZakCgiFormElementPrivate *priv;
+
+       priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       priv->value = value;
+}
+
+/**
+ * zak_cgi_form_element_get_value:
+ * @element:
+ *
+ */
+GValue
+*zak_cgi_form_element_get_value (ZakCgiFormElement *element)
+{
+       ZakCgiFormElementPrivate *priv;
+
+       priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       return priv->value;
+}
+
 /**
  * zak_cgi_form_element_render:
  * @element:
index 32f15e4acd2b8448b2d0da09eb4763f23241b571..8602e4b9039882643ebce90e83632a7dc4fec3c9 100644 (file)
@@ -62,6 +62,9 @@ gchar *zak_cgi_form_element_get_id (ZakCgiFormElement *element);
 void zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex);
 gchar *zak_cgi_form_element_get_validation_regex (ZakCgiFormElement *element);
 
+void zak_cgi_form_element_set_value (ZakCgiFormElement *element, GValue *value);
+GValue *zak_cgi_form_element_get_value (ZakCgiFormElement *element);
+
 gchar *zak_cgi_form_element_render (ZakCgiFormElement *element);
 
 gboolean zak_cgi_form_element_is_valid (ZakCgiFormElement *element);
index e791862528d78b2c8d3c10a9082a4d5ed32fbefb..aebbd1f245301a85700bb0bc691a962c352a5711 100644 (file)
 int
 main (int argc, char *argv[])
 {
+       ZakCgiMain *zakcgimain;
+
        GString *str;
 
        ZakCgiForm *form;
        ZakCgiFormElement *element;
 
-       form = zak_cgi_form_new (NULL,
+       zakcgimain = zak_cgi_main_new ();
+
+       str = g_string_new ("<html>\n"
+                                               "<head><title>Form test</title></head>\n"
+                                               "<body>\n");
+
+       form = zak_cgi_form_new (zakcgimain,
                                                         "method", "post",
+                                                        "action", "form",
                                                         NULL);
 
-       str = g_string_new ("");
-
        element = zak_cgi_form_element_text_new ("first", "aaa", NULL);
        zak_cgi_form_add_element (form, element);
 
@@ -43,7 +50,16 @@ main (int argc, char *argv[])
        element = zak_cgi_form_element_submit_new ("submit", "aaa", NULL);
        zak_cgi_form_add_element (form, element);
 
-       g_string_append (str, zak_cgi_form_render (form));
+       if (zak_cgi_main_is_post (zakcgimain))
+               {
+                       /* validating the form */
+               }
+       else
+               {
+                       g_string_append (str, zak_cgi_form_render (form));
+               }
+
+       g_string_append (str, "\n</body>");
 
        zak_cgi_main_out (NULL, str->str);