From: Andrea Zagli Date: Thu, 8 Oct 2015 22:25:23 +0000 (+0200) Subject: Added and implemented ZakCgiFormElementIValidator. X-Git-Tag: v0.1.0~3^2~32 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=a26de64ad88df696afd246c4565b8084592b382f;p=libzakcgi Added and implemented ZakCgiFormElementIValidator. --- diff --git a/src/Makefile.am b/src/Makefile.am index a4c1564..d5f4cae 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,6 +19,8 @@ libzakcgi_la_SOURCES = commons.c \ formelementsubmit.c \ formelementifilter.c \ formelementfiltertrim.c \ + formelementivalidator.c \ + formelementvalidatornotempty.c \ main.c \ session.c \ tag.c \ @@ -40,6 +42,8 @@ libzakcgi_include_HEADERS = \ formelementsubmit.h \ formelementifilter.h \ formelementfiltertrim.h \ + formelementivalidator.h \ + formelementvalidatornotempty.h \ main.h \ session.h \ tag.h \ diff --git a/src/formelement.c b/src/formelement.c index ef9c371..dd93d2e 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -28,8 +28,7 @@ enum { - PROP_0, - PROP_VALIDATION_REGEX + PROP_0 }; static void zak_cgi_form_element_class_init (ZakCgiFormElementClass *class); @@ -37,7 +36,6 @@ static void zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element); static void zak_cgi_form_element_construct (ZakCgiFormElement *element, const gchar *id, - const gchar *validation_regex, GHashTable *ht_attrs); static GHashTable *zak_cgi_form_element_get_ht_attrs (ZakCgiFormElement *element); @@ -60,9 +58,9 @@ typedef struct _ZakCgiFormElementPrivate ZakCgiFormElementPrivate; struct _ZakCgiFormElementPrivate { gchar *id; - gchar *validation_regex; GHashTable *ht_attrs; GPtrArray *pa_filters; + GPtrArray *pa_validators; GValue *value; GHashTable *ht_label_attrs; }; @@ -81,13 +79,6 @@ zak_cgi_form_element_class_init (ZakCgiFormElementClass *class) g_type_class_add_private (object_class, sizeof (ZakCgiFormElementPrivate)); - g_object_class_install_property (object_class, PROP_VALIDATION_REGEX, - g_param_spec_string ("validation-regex", - "Validation regex", - "Validation regex", - "", - G_PARAM_READWRITE)); - class->construct = zak_cgi_form_element_construct; class->get_ht_attrs = zak_cgi_form_element_get_ht_attrs; } @@ -97,9 +88,9 @@ zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element) { ZakCgiFormElementPrivate *priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (zak_cgi_form_element); - priv->validation_regex = NULL; priv->ht_attrs = NULL; priv->pa_filters = NULL; + priv->pa_validators = NULL; priv->value = NULL; priv->ht_label_attrs = NULL; } @@ -114,48 +105,6 @@ gchar return g_strdup (priv->id); } -void -zak_cgi_form_element_set_validation_regex (ZakCgiFormElement *element, const gchar *validation_regex) -{ - ZakCgiFormElementPrivate *priv; - - priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); - - if (validation_regex == NULL) - { - if (priv->validation_regex != NULL) - { - g_free (priv->validation_regex); - } - priv->validation_regex = NULL; - } - else - { - priv->validation_regex = g_strdup (validation_regex); - } -} - -gchar -*zak_cgi_form_element_get_validation_regex (ZakCgiFormElement *element) -{ - gchar *ret; - - ZakCgiFormElementPrivate *priv; - - priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); - - if (priv->validation_regex != NULL) - { - ret = g_strdup (priv->validation_regex); - } - else - { - ret = NULL; - } - - return ret; -} - /** * zak_cgi_form_element_add_filter: * @element: @@ -198,10 +147,13 @@ zak_cgi_form_element_filter (ZakCgiFormElement *element) for (i = 0; i < priv->pa_filters->len; i++) { + GValue *value; GValue *val; + value = zak_cgi_form_element_get_value (element); + val = zak_cgi_form_element_ifilter_filter ((ZakCgiFormElementIFilter *)g_ptr_array_index (priv->pa_filters, i), - zak_cgi_form_element_get_value (element)); + value); zak_cgi_form_element_set_value (element, val); } } @@ -320,6 +272,27 @@ gchar return ret; } +/** + * zak_cgi_form_element_add_validator: + * @element: + * @validator: + * + */ +void +zak_cgi_form_element_add_validator (ZakCgiFormElement *element, ZakCgiFormElementIValidator *validator) +{ + ZakCgiFormElementPrivate *priv; + + priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); + + if (priv->pa_validators == NULL) + { + priv->pa_validators = g_ptr_array_new (); + } + + g_ptr_array_add (priv->pa_validators, validator); +} + /** * zak_cgi_form_element_is_valid: * @element: @@ -330,14 +303,38 @@ zak_cgi_form_element_is_valid (ZakCgiFormElement *element) { gboolean ret; - ret = FALSE; + GValue *value; + guint i; - if (ZAK_CGI_IS_FORM_ELEMENT (element) && ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid != NULL) + ZakCgiFormElementPrivate *priv; + + priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); + + ret = TRUE; + + zak_cgi_form_element_filter (element); + + if (priv->pa_validators != NULL) { - zak_cgi_form_element_filter (element); - ret = ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid (element); + value = zak_cgi_form_element_get_value (element); + + for (i = 0; i < priv->pa_validators->len; i++) + { + if (!zak_cgi_form_element_ivalidator_validate ((ZakCgiFormElementIValidator *)g_ptr_array_index (priv->pa_validators, i), + value)) + { + ret = FALSE; + } + } } + if (ZAK_CGI_IS_FORM_ELEMENT (element) && ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid != NULL) + { + if (!ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->is_valid (element)) + { + ret = FALSE; + } + } return ret; } @@ -345,7 +342,6 @@ zak_cgi_form_element_is_valid (ZakCgiFormElement *element) static void zak_cgi_form_element_construct (ZakCgiFormElement *element, const gchar *id, - const gchar *validation_regex, GHashTable *ht_attrs) { ZakCgiFormElementPrivate *priv; @@ -356,8 +352,6 @@ zak_cgi_form_element_construct (ZakCgiFormElement *element, priv->id = g_strdup (id); - zak_cgi_form_element_set_validation_regex (element, validation_regex); - priv->ht_attrs = ht_attrs; if (g_hash_table_lookup (priv->ht_attrs, "name") == NULL) @@ -389,10 +383,6 @@ zak_cgi_form_element_set_property (GObject *object, switch (property_id) { - case PROP_VALIDATION_REGEX: - zak_cgi_form_element_set_validation_regex (zak_cgi_form_element, g_value_get_string (value)); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -410,10 +400,6 @@ zak_cgi_form_element_get_property (GObject *object, switch (property_id) { - case PROP_VALIDATION_REGEX: - g_value_set_string (value, zak_cgi_form_element_get_validation_regex (zak_cgi_form_element)); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; diff --git a/src/formelement.h b/src/formelement.h index a233fc5..34a8900 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -24,6 +24,7 @@ #include "main.h" #include "formelementifilter.h" +#include "formelementivalidator.h" G_BEGIN_DECLS @@ -48,7 +49,7 @@ struct _ZakCgiFormElementClass { GObjectClass parent_class; - void (*construct) (ZakCgiFormElement *element, const gchar *id, const gchar *validation_regex, GHashTable *ht_attrs); + void (*construct) (ZakCgiFormElement *element, const gchar *id, GHashTable *ht_attrs); GHashTable *(*get_ht_attrs) (ZakCgiFormElement *element); gchar *(*render) (ZakCgiFormElement *element); @@ -60,9 +61,6 @@ GType zak_cgi_form_element_get_type (void); 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_add_filter (ZakCgiFormElement *element, ZakCgiFormElementIFilter *filter); void zak_cgi_form_element_filter (ZakCgiFormElement *element); @@ -73,6 +71,7 @@ void zak_cgi_form_element_set_label (ZakCgiFormElement *element, const gchar *la gchar *zak_cgi_form_element_render (ZakCgiFormElement *element); +void zak_cgi_form_element_add_validator (ZakCgiFormElement *element, ZakCgiFormElementIValidator *validator); gboolean zak_cgi_form_element_is_valid (ZakCgiFormElement *element); diff --git a/src/formelementcheck.c b/src/formelementcheck.c index bcf41bb..fc5d82a 100644 --- a/src/formelementcheck.c +++ b/src/formelementcheck.c @@ -67,7 +67,6 @@ zak_cgi_form_element_check_class_init (ZakCgiFormElementCheckClass *klass) object_class->finalize = zak_cgi_form_element_check_finalize; elem_class->render = zak_cgi_form_element_check_render; - elem_class->is_valid = zak_cgi_form_element_check_is_valid; g_type_class_add_private (object_class, sizeof (ZakCgiFormElementCheckPrivate)); } @@ -82,15 +81,13 @@ zak_cgi_form_element_check_init (ZakCgiFormElementCheck *zak_cgi_form_element_ch /** * zak_cgi_form_element_check_new: * @id: - * @validation_regex: * @...: * * Returns: the newly created #ZakCgiFormElementCheck object. */ ZakCgiFormElement *zak_cgi_form_element_check_new (const gchar *id, - const gchar *validation_regex, - ...) + ...) { va_list ap; @@ -100,14 +97,13 @@ ZakCgiFormElement zak_cgi_form_element_check = ZAK_CGI_FORM_ELEMENT_CHECK (g_object_new (zak_cgi_form_element_check_get_type (), NULL)); - va_start (ap, validation_regex); + va_start (ap, id); ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap); g_hash_table_replace (ht_attrs, "type", "checkbox"); ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_check_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_check), id, - validation_regex, ht_attrs); return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_check); @@ -139,72 +135,6 @@ static gchar return ret; } -static gboolean -zak_cgi_form_element_check_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_check_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_check_check_value (str_regex, (GValue *)g_ptr_array_index (ar, i))) - { - ret = FALSE; - break; - } - } - } - else - { - ret = zak_cgi_form_element_check_check_value (str_regex, gval); - } - - return ret; -} - /* PRIVATE */ static void zak_cgi_form_element_check_set_property (GObject *object, diff --git a/src/formelementcheck.h b/src/formelementcheck.h index dd1c80b..1d56774 100644 --- a/src/formelementcheck.h +++ b/src/formelementcheck.h @@ -51,7 +51,7 @@ struct _ZakCgiFormElementCheckClass GType zak_cgi_form_element_check_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_check_new (const gchar *id, const gchar *validation_regex, ...); +ZakCgiFormElement *zak_cgi_form_element_check_new (const gchar *id, ...); G_END_DECLS diff --git a/src/formelementhidden.c b/src/formelementhidden.c index 0408cef..f2832b1 100644 --- a/src/formelementhidden.c +++ b/src/formelementhidden.c @@ -31,8 +31,6 @@ static void zak_cgi_form_element_hidden_init (ZakCgiFormElementHidden *zak_cgi_f static gchar *zak_cgi_form_element_hidden_render (ZakCgiFormElement *element); -static gboolean zak_cgi_form_element_hidden_is_valid (ZakCgiFormElement *element); - static void zak_cgi_form_element_hidden_set_property (GObject *object, guint property_id, const GValue *value, @@ -67,7 +65,6 @@ zak_cgi_form_element_hidden_class_init (ZakCgiFormElementHiddenClass *klass) object_class->finalize = zak_cgi_form_element_hidden_finalize; elem_class->render = zak_cgi_form_element_hidden_render; - elem_class->is_valid = zak_cgi_form_element_hidden_is_valid; g_type_class_add_private (object_class, sizeof (ZakCgiFormElementHiddenPrivate)); } @@ -82,15 +79,13 @@ zak_cgi_form_element_hidden_init (ZakCgiFormElementHidden *zak_cgi_form_element_ /** * zak_cgi_form_element_hidden_new: * @id: - * @validation_regex: * @...: * * Returns: the newly created #ZakCgiFormElementHidden object. */ ZakCgiFormElement *zak_cgi_form_element_hidden_new (const gchar *id, - const gchar *validation_regex, - ...) + ...) { va_list ap; @@ -100,14 +95,13 @@ ZakCgiFormElement zak_cgi_form_element_hidden = ZAK_CGI_FORM_ELEMENT_HIDDEN (g_object_new (zak_cgi_form_element_hidden_get_type (), NULL)); - va_start (ap, validation_regex); + va_start (ap, id); ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap); g_hash_table_replace (ht_attrs, "type", "hidden"); ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_hidden_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_hidden), id, - validation_regex, ht_attrs); return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_hidden); @@ -139,72 +133,6 @@ static gchar return ret; } -static gboolean -zak_cgi_form_element_hidden_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_hidden_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_hidden_check_value (str_regex, (GValue *)g_ptr_array_index (ar, i))) - { - ret = FALSE; - break; - } - } - } - else - { - ret = zak_cgi_form_element_hidden_check_value (str_regex, gval); - } - - return ret; -} - /* PRIVATE */ static void zak_cgi_form_element_hidden_set_property (GObject *object, diff --git a/src/formelementhidden.h b/src/formelementhidden.h index 86935f5..4f9b3de 100644 --- a/src/formelementhidden.h +++ b/src/formelementhidden.h @@ -51,7 +51,7 @@ struct _ZakCgiFormElementHiddenClass GType zak_cgi_form_element_hidden_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_hidden_new (const gchar *id, const gchar *validation_regex, ...); +ZakCgiFormElement *zak_cgi_form_element_hidden_new (const gchar *id, ...); G_END_DECLS diff --git a/src/formelementivalidator.c b/src/formelementivalidator.c new file mode 100644 index 0000000..21b9734 --- /dev/null +++ b/src/formelementivalidator.c @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "formelementivalidator.h" + +G_DEFINE_INTERFACE (ZakCgiFormElementIValidator, zak_cgi_form_element_ivalidator, G_TYPE_OBJECT); + +static void +zak_cgi_form_element_ivalidator_default_init (ZakCgiFormElementIValidatorInterface *iface) +{ + /* add properties and signals to the interface here */ +} + +gboolean +zak_cgi_form_element_ivalidator_validate (ZakCgiFormElementIValidator *self, GValue *value) +{ + g_return_if_fail (ZAK_CGI_IS_FORM_ELEMENT_IVALIDATOR (self)); + + return ZAK_CGI_FORM_ELEMENT_IVALIDATOR_GET_IFACE (self)->validate (self, value); +} diff --git a/src/formelementivalidator.h b/src/formelementivalidator.h new file mode 100644 index 0000000..cbce915 --- /dev/null +++ b/src/formelementivalidator.h @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __ZAK_CGI_FORM_ELEMENT_IVALIDATOR_H__ +#define __ZAK_CGI_FORM_ELEMENT_IVALIDATOR_H__ + +#include + + +G_BEGIN_DECLS + + +#define ZAK_CGI_TYPE_FORM_ELEMENT_IVALIDATOR zak_cgi_form_element_ivalidator_get_type () +G_DECLARE_INTERFACE (ZakCgiFormElementIValidator, zak_cgi_form_element_ivalidator, ZAK_CGI, FORM_ELEMENT_IVALIDATOR, GObject) + +struct _ZakCgiFormElementIValidatorInterface +{ + GTypeInterface parent_iface; + + gboolean (*validate) (ZakCgiFormElementIValidator *self, GValue *value); +}; + +gboolean zak_cgi_form_element_ivalidator_validate (ZakCgiFormElementIValidator *self, GValue *value); + + +G_END_DECLS + + +#endif /* __ZAK_CGI_FOR_ELEMENT_IVALIDATOR_H__ */ diff --git a/src/formelementpassword.c b/src/formelementpassword.c index 2c32fe7..88d2c33 100644 --- a/src/formelementpassword.c +++ b/src/formelementpassword.c @@ -31,8 +31,6 @@ static void zak_cgi_form_element_password_init (ZakCgiFormElementPassword *zak_c static gchar *zak_cgi_form_element_password_render (ZakCgiFormElement *element); -static gboolean zak_cgi_form_element_password_is_valid (ZakCgiFormElement *element); - static void zak_cgi_form_element_password_set_property (GObject *object, guint property_id, const GValue *value, @@ -67,7 +65,6 @@ zak_cgi_form_element_password_class_init (ZakCgiFormElementPasswordClass *klass) object_class->finalize = zak_cgi_form_element_password_finalize; elem_class->render = zak_cgi_form_element_password_render; - elem_class->is_valid = zak_cgi_form_element_password_is_valid; g_type_class_add_private (object_class, sizeof (ZakCgiFormElementPasswordPrivate)); } @@ -82,14 +79,12 @@ zak_cgi_form_element_password_init (ZakCgiFormElementPassword *zak_cgi_form_elem /** * zak_cgi_form_element_password_new: * @id: - * @validation_regex: * @...: * * Returns: the newly created #ZakCgiFormElementPassword object. */ ZakCgiFormElement *zak_cgi_form_element_password_new (const gchar *id, - const gchar *validation_regex, ...) { va_list ap; @@ -100,14 +95,13 @@ ZakCgiFormElement zak_cgi_form_element_password = ZAK_CGI_FORM_ELEMENT_PASSWORD (g_object_new (zak_cgi_form_element_password_get_type (), NULL)); - va_start (ap, validation_regex); + va_start (ap, id); ht_attrs = zak_cgi_commons_valist_to_ghashtable (ap); g_hash_table_replace (ht_attrs, "type", "password"); ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_password_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_password), id, - validation_regex, ht_attrs); return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_password); @@ -139,72 +133,6 @@ static gchar return ret; } -static gboolean -zak_cgi_form_element_password_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_password_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_password_check_value (str_regex, (GValue *)g_ptr_array_index (ar, i))) - { - ret = FALSE; - break; - } - } - } - else - { - ret = zak_cgi_form_element_password_check_value (str_regex, gval); - } - - return ret; -} - /* PRIVATE */ static void zak_cgi_form_element_password_set_property (GObject *object, diff --git a/src/formelementpassword.h b/src/formelementpassword.h index e1fe765..dcba599 100644 --- a/src/formelementpassword.h +++ b/src/formelementpassword.h @@ -51,7 +51,7 @@ struct _ZakCgiFormElementPasswordClass GType zak_cgi_form_element_password_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_password_new (const gchar *id, const gchar *validation_regex, ...); +ZakCgiFormElement *zak_cgi_form_element_password_new (const gchar *id, ...); G_END_DECLS diff --git a/src/formelementselect.c b/src/formelementselect.c index 4a6f955..6f6e90c 100644 --- a/src/formelementselect.c +++ b/src/formelementselect.c @@ -31,8 +31,6 @@ static void zak_cgi_form_element_select_init (ZakCgiFormElementSelect *zak_cgi_f static gchar *zak_cgi_form_element_select_render (ZakCgiFormElement *element); -static gboolean zak_cgi_form_element_select_is_valid (ZakCgiFormElement *element); - static void zak_cgi_form_element_select_set_property (GObject *object, guint property_id, const GValue *value, @@ -67,7 +65,6 @@ zak_cgi_form_element_select_class_init (ZakCgiFormElementSelectClass *klass) object_class->finalize = zak_cgi_form_element_select_finalize; elem_class->render = zak_cgi_form_element_select_render; - elem_class->is_valid = zak_cgi_form_element_select_is_valid; g_type_class_add_private (object_class, sizeof (ZakCgiFormElementSelectPrivate)); } @@ -83,14 +80,12 @@ zak_cgi_form_element_select_init (ZakCgiFormElementSelect *zak_cgi_form_element_ /** * zak_cgi_form_element_select_new: * @id: - * @validation_regex: * @...: * * Returns: the newly created #ZakCgiFormElementSelect object. */ ZakCgiFormElement *zak_cgi_form_element_select_new (const gchar *id, - const gchar *validation_regex, ...) { va_list ap; @@ -99,11 +94,10 @@ ZakCgiFormElement zak_cgi_form_element_select = ZAK_CGI_FORM_ELEMENT_SELECT (g_object_new (zak_cgi_form_element_select_get_type (), NULL)); - va_start (ap, validation_regex); + va_start (ap, id); ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_select_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_select), id, - validation_regex, zak_cgi_commons_valist_to_ghashtable (ap)); return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_select); @@ -193,72 +187,6 @@ static gchar return ret; } -static gboolean -zak_cgi_form_element_select_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 != NULL && 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_select_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_select_check_value (str_regex, (GValue *)g_ptr_array_index (ar, i))) - { - ret = FALSE; - break; - } - } - } - else - { - ret = zak_cgi_form_element_select_check_value (str_regex, gval); - } - - return ret; -} - /* PRIVATE */ static void zak_cgi_form_element_select_set_property (GObject *object, diff --git a/src/formelementselect.h b/src/formelementselect.h index d7ae039..fdbc453 100644 --- a/src/formelementselect.h +++ b/src/formelementselect.h @@ -51,7 +51,7 @@ struct _ZakCgiFormElementSelectClass GType zak_cgi_form_element_select_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_select_new (const gchar *id, const gchar *validation_regex, ...); +ZakCgiFormElement *zak_cgi_form_element_select_new (const gchar *id, ...); void zak_cgi_form_element_select_add_option (ZakCgiFormElementSelect *element, const gchar *value, const gchar *content, ...); diff --git a/src/formelementsubmit.c b/src/formelementsubmit.c index 44dca46..0a80ab0 100644 --- a/src/formelementsubmit.c +++ b/src/formelementsubmit.c @@ -31,8 +31,6 @@ static void zak_cgi_form_element_submit_init (ZakCgiFormElementSubmit *zak_cgi_f static gchar *zak_cgi_form_element_submit_render (ZakCgiFormElement *element); -static gboolean zak_cgi_form_element_submit_is_valid (ZakCgiFormElement *element); - static void zak_cgi_form_element_submit_set_property (GObject *object, guint property_id, const GValue *value, @@ -67,7 +65,6 @@ zak_cgi_form_element_submit_class_init (ZakCgiFormElementSubmitClass *klass) object_class->finalize = zak_cgi_form_element_submit_finalize; elem_class->render = zak_cgi_form_element_submit_render; - elem_class->is_valid = zak_cgi_form_element_submit_is_valid; g_type_class_add_private (object_class, sizeof (ZakCgiFormElementSubmitPrivate)); } @@ -82,15 +79,13 @@ zak_cgi_form_element_submit_init (ZakCgiFormElementSubmit *zak_cgi_form_element_ /** * zak_cgi_form_element_submit_new: * @id: - * @validation_regex: * @...: * * Returns: the newly created #ZakCgiFormElementSubmit object. */ ZakCgiFormElement *zak_cgi_form_element_submit_new (const gchar *id, - const gchar *validation_regex, - ...) + ...) { va_list ap; @@ -98,12 +93,11 @@ ZakCgiFormElement zak_cgi_form_element_submit = ZAK_CGI_FORM_ELEMENT_SUBMIT (g_object_new (zak_cgi_form_element_submit_get_type (), NULL)); - va_start (ap, validation_regex); + va_start (ap, id); ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_submit_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_submit), - id, - validation_regex, - zak_cgi_commons_valist_to_ghashtable (ap)); + id, + zak_cgi_commons_valist_to_ghashtable (ap)); return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_submit); } @@ -153,12 +147,6 @@ static gchar return ret; } -static gboolean -zak_cgi_form_element_submit_is_valid (ZakCgiFormElement *element) -{ - return TRUE; -} - /* PRIVATE */ static void zak_cgi_form_element_submit_set_property (GObject *object, diff --git a/src/formelementsubmit.h b/src/formelementsubmit.h index c9ce80a..8f71b15 100644 --- a/src/formelementsubmit.h +++ b/src/formelementsubmit.h @@ -51,7 +51,7 @@ struct _ZakCgiFormElementSubmitClass GType zak_cgi_form_element_submit_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_submit_new (const gchar *id, const gchar *validation_regex, ...); +ZakCgiFormElement *zak_cgi_form_element_submit_new (const gchar *id, ...); G_END_DECLS diff --git a/src/formelementtext.c b/src/formelementtext.c index bbdcf66..af2dc21 100644 --- a/src/formelementtext.c +++ b/src/formelementtext.c @@ -67,7 +67,7 @@ zak_cgi_form_element_text_class_init (ZakCgiFormElementTextClass *klass) object_class->finalize = zak_cgi_form_element_text_finalize; elem_class->render = zak_cgi_form_element_text_render; - elem_class->is_valid = zak_cgi_form_element_text_is_valid; + //elem_class->is_valid = zak_cgi_form_element_text_is_valid; g_type_class_add_private (object_class, sizeof (ZakCgiFormElementTextPrivate)); } @@ -82,14 +82,12 @@ zak_cgi_form_element_text_init (ZakCgiFormElementText *zak_cgi_form_element_text /** * zak_cgi_form_element_text_new: * @id: - * @validation_regex: * @...: * * Returns: the newly created #ZakCgiFormElementText object. */ ZakCgiFormElement *zak_cgi_form_element_text_new (const gchar *id, - const gchar *validation_regex, ...) { va_list ap; @@ -98,11 +96,10 @@ ZakCgiFormElement zak_cgi_form_element_text = ZAK_CGI_FORM_ELEMENT_TEXT (g_object_new (zak_cgi_form_element_text_get_type (), NULL)); - va_start (ap, validation_regex); + va_start (ap, id); ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text), id, - validation_regex, zak_cgi_commons_valist_to_ghashtable (ap)); return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text); diff --git a/src/formelementtext.h b/src/formelementtext.h index 957c687..5abdfb6 100644 --- a/src/formelementtext.h +++ b/src/formelementtext.h @@ -51,7 +51,7 @@ struct _ZakCgiFormElementTextClass GType zak_cgi_form_element_text_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_text_new (const gchar *id, const gchar *validation_regex, ...); +ZakCgiFormElement *zak_cgi_form_element_text_new (const gchar *id, ...); G_END_DECLS diff --git a/src/formelementtextarea.c b/src/formelementtextarea.c index ad02655..22be71e 100644 --- a/src/formelementtextarea.c +++ b/src/formelementtextarea.c @@ -31,8 +31,6 @@ static void zak_cgi_form_element_text_area_init (ZakCgiFormElementTextArea *zak_ static gchar *zak_cgi_form_element_text_area_render (ZakCgiFormElement *element); -static gboolean zak_cgi_form_element_text_area_is_valid (ZakCgiFormElement *element); - static void zak_cgi_form_element_text_area_set_property (GObject *object, guint property_id, const GValue *value, @@ -67,7 +65,6 @@ zak_cgi_form_element_text_area_class_init (ZakCgiFormElementTextAreaClass *klass object_class->finalize = zak_cgi_form_element_text_area_finalize; elem_class->render = zak_cgi_form_element_text_area_render; - elem_class->is_valid = zak_cgi_form_element_text_area_is_valid; g_type_class_add_private (object_class, sizeof (ZakCgiFormElementTextAreaPrivate)); } @@ -82,15 +79,13 @@ zak_cgi_form_element_text_area_init (ZakCgiFormElementTextArea *zak_cgi_form_ele /** * zak_cgi_form_element_text_area_new: * @id: - * @validation_regex: * @...: * * Returns: the newly created #ZakCgiFormElementTextArea object. */ ZakCgiFormElement *zak_cgi_form_element_text_area_new (const gchar *id, - const gchar *validation_regex, - ...) + ...) { va_list ap; @@ -98,12 +93,11 @@ ZakCgiFormElement zak_cgi_form_element_text_area = ZAK_CGI_FORM_ELEMENT_TEXT_AREA (g_object_new (zak_cgi_form_element_text_area_get_type (), NULL)); - va_start (ap, validation_regex); + va_start (ap, id); ZAK_CGI_FORM_ELEMENT_CLASS (zak_cgi_form_element_text_area_parent_class)->construct (ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text_area), - id, - validation_regex, - zak_cgi_commons_valist_to_ghashtable (ap)); + id, + zak_cgi_commons_valist_to_ghashtable (ap)); return ZAK_CGI_FORM_ELEMENT (zak_cgi_form_element_text_area); } @@ -134,72 +128,6 @@ static gchar return ret; } -static gboolean -zak_cgi_form_element_text_area_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_area_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_area_check_value (str_regex, (GValue *)g_ptr_array_index (ar, i))) - { - ret = FALSE; - break; - } - } - } - else - { - ret = zak_cgi_form_element_text_area_check_value (str_regex, gval); - } - - return ret; -} - /* PRIVATE */ static void zak_cgi_form_element_text_area_set_property (GObject *object, diff --git a/src/formelementtextarea.h b/src/formelementtextarea.h index 03f132f..abbf253 100644 --- a/src/formelementtextarea.h +++ b/src/formelementtextarea.h @@ -51,7 +51,7 @@ struct _ZakCgiFormElementTextAreaClass GType zak_cgi_form_element_text_area_get_type (void); -ZakCgiFormElement *zak_cgi_form_element_text_area_new (const gchar *id, const gchar *validation_regex, ...); +ZakCgiFormElement *zak_cgi_form_element_text_area_new (const gchar *id, ...); G_END_DECLS diff --git a/src/formelementvalidatornotempty.c b/src/formelementvalidatornotempty.c new file mode 100644 index 0000000..c2e1a8d --- /dev/null +++ b/src/formelementvalidatornotempty.c @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include "formelementivalidator.h" +#include "formelementvalidatornotempty.h" + +static void zak_cgi_form_element_validator_notempty_class_init (ZakCgiFormElementValidatorNotemptyClass *class); +static void zak_cgi_form_element_validator_notempty_interface_init (ZakCgiFormElementIValidatorInterface *iface); +static void zak_cgi_form_element_validator_notempty_init (ZakCgiFormElementValidatorNotempty *zak_cgi_form_element); + +static void zak_cgi_form_element_validator_notempty_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_cgi_form_element_validator_notempty_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_cgi_form_element_validator_notempty_dispose (GObject *gobject); +static void zak_cgi_form_element_validator_notempty_finalize (GObject *gobject); + +static gboolean zak_cgi_form_element_validator_notempty_validate (ZakCgiFormElementIValidator *validator_notempty, GValue *value); + +struct _ZakCgiFormElementValidatorNotempty +{ + GObject parent_instance; + + /* Other members, including private data. */ +}; + +#define ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_CGI_TYPE_FORM_ELEMENT_VALIDATOR_NOTEMPTY, ZakCgiFormElementValidatorNotemptyPrivate)) + +typedef struct _ZakCgiFormElementValidatorNotemptyPrivate ZakCgiFormElementValidatorNotemptyPrivate; +struct _ZakCgiFormElementValidatorNotemptyPrivate + { + gpointer nothing; + }; + +G_DEFINE_TYPE_WITH_CODE (ZakCgiFormElementValidatorNotempty, zak_cgi_form_element_validator_notempty, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (ZAK_CGI_TYPE_FORM_ELEMENT_IVALIDATOR, + zak_cgi_form_element_validator_notempty_interface_init)) + +static void +zak_cgi_form_element_validator_notempty_class_init (ZakCgiFormElementValidatorNotemptyClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = zak_cgi_form_element_validator_notempty_set_property; + object_class->get_property = zak_cgi_form_element_validator_notempty_get_property; + object_class->dispose = zak_cgi_form_element_validator_notempty_dispose; + object_class->finalize = zak_cgi_form_element_validator_notempty_finalize; + + g_type_class_add_private (object_class, sizeof (ZakCgiFormElementValidatorNotemptyPrivate)); +} + +static void +zak_cgi_form_element_validator_notempty_interface_init (ZakCgiFormElementIValidatorInterface *iface) +{ + iface->validate = zak_cgi_form_element_validator_notempty_validate; +} + +static void +zak_cgi_form_element_validator_notempty_init (ZakCgiFormElementValidatorNotempty *zak_cgi_form_element) +{ + ZakCgiFormElementValidatorNotemptyPrivate *priv = ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_GET_PRIVATE (zak_cgi_form_element); +} + +/** + * zak_cgi_form_element_validator_notempty_new: + * + * Returns: the newly created #ZakCgiFormElementValidatorNotempty object. + */ +ZakCgiFormElementValidatorNotempty +*zak_cgi_form_element_validator_notempty_new () +{ + ZakCgiFormElementValidatorNotempty *zak_cgi_form_element_validator_notempty; + + zak_cgi_form_element_validator_notempty = ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY (g_object_new (zak_cgi_form_element_validator_notempty_get_type (), NULL)); + + return zak_cgi_form_element_validator_notempty; +} + +/* PRIVATE */ +static void +zak_cgi_form_element_validator_notempty_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakCgiFormElementValidatorNotempty *zak_cgi_form_element = (ZakCgiFormElementValidatorNotempty *)object; + ZakCgiFormElementValidatorNotemptyPrivate *priv = ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_GET_PRIVATE (zak_cgi_form_element); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_cgi_form_element_validator_notempty_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakCgiFormElementValidatorNotempty *zak_cgi_form_element = (ZakCgiFormElementValidatorNotempty *)object; + ZakCgiFormElementValidatorNotemptyPrivate *priv = ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_GET_PRIVATE (zak_cgi_form_element); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_cgi_form_element_validator_notempty_dispose (GObject *gobject) +{ + ZakCgiFormElementValidatorNotempty *zak_cgi_form_element = (ZakCgiFormElementValidatorNotempty *)gobject; + ZakCgiFormElementValidatorNotemptyPrivate *priv = ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_GET_PRIVATE (zak_cgi_form_element); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_cgi_form_element_validator_notempty_finalize (GObject *gobject) +{ + ZakCgiFormElementValidatorNotempty *zak_cgi_form_element = (ZakCgiFormElementValidatorNotempty *)gobject; + ZakCgiFormElementValidatorNotemptyPrivate *priv = ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_GET_PRIVATE (zak_cgi_form_element); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} + +static gboolean +zak_cgi_form_element_validator_notempty_validate (ZakCgiFormElementIValidator *validator_notempty, + GValue *value) +{ + gboolean ret; + + g_return_val_if_fail (value != NULL, FALSE); + + ret = (g_strcmp0 (g_value_get_string (value), "") != 0); + + return ret; +} diff --git a/src/formelementvalidatornotempty.h b/src/formelementvalidatornotempty.h new file mode 100644 index 0000000..3cda157 --- /dev/null +++ b/src/formelementvalidatornotempty.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_H__ +#define __ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_H__ + + +#include + + +G_BEGIN_DECLS + + +#define ZAK_CGI_TYPE_FORM_ELEMENT_VALIDATOR_NOTEMPTY zak_cgi_form_element_validator_notempty_get_type () +G_DECLARE_FINAL_TYPE (ZakCgiFormElementValidatorNotempty, zak_cgi_form_element_validator_notempty, ZAK_CGI, FORM_ELEMENT_VALIDATOR_NOTEMPTY, GObject) + +ZakCgiFormElementValidatorNotempty *zak_cgi_form_element_validator_notempty_new (void); + + +G_END_DECLS + + +#endif /* __ZAK_CGI_FORM_ELEMENT_VALIDATOR_NOTEMPTY_H__ */ diff --git a/tests/form.c b/tests/form.c index b436fcb..05ae5aa 100644 --- a/tests/form.c +++ b/tests/form.c @@ -29,6 +29,8 @@ #include #include #include +#include +#include int main (int argc, char *argv[]) @@ -61,41 +63,42 @@ main (int argc, char *argv[]) "action", "form", NULL); - element = zak_cgi_form_element_text_new ("first", "aaa", NULL); + element = zak_cgi_form_element_text_new ("first", NULL); zak_cgi_form_element_set_label (element, "The Label for first", NULL); zak_cgi_form_element_add_filter (element, ZAK_CGI_FORM_ELEMENT_IFILTER (zak_cgi_form_element_filter_trim_new ())); + zak_cgi_form_element_add_validator (element, + ZAK_CGI_FORM_ELEMENT_IVALIDATOR (zak_cgi_form_element_validator_notempty_new ())); zak_cgi_form_add_element (form, element); - element = zak_cgi_form_element_check_new ("chk", NULL, NULL); + element = zak_cgi_form_element_check_new ("chk", NULL); zak_cgi_form_element_set_label (element, "The checkbox", NULL); zak_cgi_form_add_element (form, element); zak_cgi_form_add_str (form, "

big big big

"); - element = zak_cgi_form_element_password_new ("pws", "aaa", NULL); + element = zak_cgi_form_element_password_new ("pws", NULL); zak_cgi_form_element_set_label (element, "The password", NULL); zak_cgi_form_add_element (form, element); - element = zak_cgi_form_element_text_area_new ("txtarea", "aaa", NULL); + element = zak_cgi_form_element_text_area_new ("txtarea", NULL); zak_cgi_form_element_set_label (element, "The text area", NULL); zak_cgi_form_add_element (form, element); - element = zak_cgi_form_element_select_new ("slc", NULL, NULL); + element = zak_cgi_form_element_select_new ("slc", NULL); zak_cgi_form_element_select_add_option (ZAK_CGI_FORM_ELEMENT_SELECT (element), "1", "first", NULL); zak_cgi_form_element_select_add_option (ZAK_CGI_FORM_ELEMENT_SELECT (element), "2", "second", NULL); zak_cgi_form_element_select_add_option (ZAK_CGI_FORM_ELEMENT_SELECT (element), "3", "third", NULL); zak_cgi_form_add_element (form, element); - element = zak_cgi_form_element_hidden_new ("hdn", "aaa", NULL); + element = zak_cgi_form_element_hidden_new ("hdn", NULL); zak_cgi_form_add_element (form, element); - element = zak_cgi_form_element_submit_new ("submit", NULL, "zak-cgi-content", "Submit", NULL); + element = zak_cgi_form_element_submit_new ("submit", "zak-cgi-content", "Submit", NULL); zak_cgi_form_add_element (form, element); if (zak_cgi_main_is_post (zakcgimain)) { - sleep(10); /* validating the form */ zak_cgi_form_bind (form); if (zak_cgi_form_is_valid (form))