From f692316498fa6cd255de7599535a9f293aa379a4 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 29 Aug 2015 11:46:30 +0200 Subject: [PATCH] Added function ZakCgiForm::bind. --- src/commons.c | 2 +- src/form.c | 36 ++++++++++++++++++++++++++++++++++++ src/form.h | 2 ++ src/formelement.c | 32 ++++++++++++++++++++++++++++++++ src/formelement.h | 3 +++ tests/form.c | 24 ++++++++++++++++++++---- 6 files changed, 94 insertions(+), 5 deletions(-) diff --git a/src/commons.c b/src/commons.c index 93c5075..421a501 100644 --- a/src/commons.c +++ b/src/commons.c @@ -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); } diff --git a/src/form.c b/src/form.c index da81760..a16e735 100644 --- a/src/form.c +++ b/src/form.c @@ -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 ("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: diff --git a/src/formelement.h b/src/formelement.h index 32f15e4..8602e4b 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -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); diff --git a/tests/form.c b/tests/form.c index e791862..aebbd1f 100644 --- a/tests/form.c +++ b/tests/form.c @@ -24,17 +24,24 @@ 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 ("\n" + "Form test\n" + "\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"); zak_cgi_main_out (NULL, str->str); -- 2.49.0