]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added ZakFormElementValidatorDate.
authorAndrea Zagli <azagli@libero.it>
Mon, 28 Dec 2015 14:05:04 +0000 (15:05 +0100)
committerAndrea Zagli <azagli@libero.it>
Mon, 28 Dec 2015 14:05:04 +0000 (15:05 +0100)
src/Makefile.am
src/form.c
src/formelementvalidatordate.c [new file with mode: 0644]
src/formelementvalidatordate.h [new file with mode: 0644]
src/formelementvalidatorregex.c

index c0380a304bac61c432e2435b7c441a15b5f6f5c4..6922a3f47e179936875fe80452443a1046cb7336 100644 (file)
@@ -13,6 +13,7 @@ libzakform_la_SOURCES = \
                         formelementfilter.c \
                         formelementfiltertrim.c \
                         formelementvalidator.c \
+                        formelementvalidatordate.c \
                         formelementvalidatornotempty.c \
                         formelementvalidatorregex.c \
                         formiprovider.c
@@ -26,6 +27,7 @@ libzakform_include_HEADERS = \
                              formelementfilter.h \
                              formelementfiltertrim.h \
                              formelementvalidator.h \
+                             formelementvalidatordate.h \
                              formelementvalidatornotempty.h \
                              formelementvalidatorregex.h \
                              formiprovider.h
index 8c5cf71e98eed446944a8a419cd4d178b50dd9ce..c0b66eb7dff99b60ce399bdb2e32dc50a37f2595 100644 (file)
@@ -199,6 +199,10 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element
                                                                                }
                                                                }
                                                }
+                                       if (i >= priv->ar_modules->len)
+                                               {
+                                                       g_warning ("Filter «%s» not found.", type);
+                                               }
                                }
                    else if (xmlStrcmp (xnode->name, (const xmlChar *)"validator") == 0)
                                {
@@ -230,6 +234,10 @@ zak_form_form_element_xml_parsing (ZakFormForm *zakform, ZakFormElement *element
                                                                                }
                                                                }
                                                }
+                                       if (i >= priv->ar_modules->len)
+                                               {
+                                                       g_warning ("Validator «%s» not found.", type);
+                                               }
                                }
 
                        xnode = xnode->next;
diff --git a/src/formelementvalidatordate.c b/src/formelementvalidatordate.c
new file mode 100644 (file)
index 0000000..2546c61
--- /dev/null
@@ -0,0 +1,237 @@
+/*
+ * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ *
+ * 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 <config.h>
+#endif
+
+#include <libzakutils/libzakutils.h>
+
+#include "formelementvalidator.h"
+#include "formelementvalidatordate.h"
+
+static void zak_form_element_validator_date_class_init (ZakFormElementValidatorDateClass *class);
+static void zak_form_element_validator_date_init (ZakFormElementValidatorDate *zak_form_element);
+
+static void zak_form_element_validator_date_set_property (GObject *object,
+                               guint property_id,
+                               const GValue *value,
+                               GParamSpec *pspec);
+static void zak_form_element_validator_date_get_property (GObject *object,
+                               guint property_id,
+                               GValue *value,
+                               GParamSpec *pspec);
+
+static void zak_form_element_validator_date_dispose (GObject *gobject);
+static void zak_form_element_validator_date_finalize (GObject *gobject);
+
+static gboolean zak_form_element_validator_date_validate (ZakFormElementValidator *validator_date, const gchar *value);
+
+struct _ZakFormElementValidatorDate
+{
+       ZakFormElementValidator parent_instance;
+
+       /* Other members, including private data. */
+};
+
+#define ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_TYPE_FORM_ELEMENT_VALIDATOR_DATE, ZakFormElementValidatorDatePrivate))
+
+typedef struct _ZakFormElementValidatorDatePrivate ZakFormElementValidatorDatePrivate;
+struct _ZakFormElementValidatorDatePrivate
+       {
+               gchar *format;
+       };
+
+G_DEFINE_TYPE (ZakFormElementValidatorDate, zak_form_element_validator_date, ZAK_FORM_TYPE_ELEMENT_VALIDATOR)
+
+static void
+zak_form_element_validator_date_class_init (ZakFormElementValidatorDateClass *class)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (class);
+       ZakFormElementValidatorClass *parent_class = ZAK_FORM_ELEMENT_VALIDATOR_CLASS (class);
+
+       object_class->set_property = zak_form_element_validator_date_set_property;
+       object_class->get_property = zak_form_element_validator_date_get_property;
+       object_class->dispose = zak_form_element_validator_date_dispose;
+       object_class->finalize = zak_form_element_validator_date_finalize;
+
+       parent_class->validate = zak_form_element_validator_date_validate;
+
+       g_type_class_add_private (object_class, sizeof (ZakFormElementValidatorDatePrivate));
+}
+
+static void
+zak_form_element_validator_date_init (ZakFormElementValidatorDate *zak_form_element)
+{
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element);
+
+       priv->format = NULL;
+}
+
+/**
+ * zak_form_element_validator_date_new:
+ *
+ * Returns: the newly created #ZakFormElementValidatorDate object.
+ */
+ZakFormElementValidatorDate
+*zak_form_element_validator_date_new ()
+{
+       ZakFormElementValidatorDate *zak_form_element_validator_date;
+
+       zak_form_element_validator_date = ZAK_FORM_ELEMENT_VALIDATOR_DATE (g_object_new (zak_form_element_validator_date_get_type (), NULL));
+
+       return zak_form_element_validator_date;
+}
+
+/**
+ * zak_form_element_validator_date_set_format:
+ * @validator:
+ * @format:
+ *
+ */
+void
+zak_form_element_validator_date_set_format (ZakFormElementValidatorDate *validator, const gchar *format)
+{
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator);
+
+       if (priv->format != NULL)
+               {
+                       g_free (priv->format);
+               }
+       priv->format = g_strdup (format);
+}
+
+/**
+ * zak_form_element_validator_date_get_format:
+ * @validator:
+ *
+ */
+gchar
+*zak_form_element_validator_date_get_format (ZakFormElementValidatorDate *validator)
+{
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator);
+
+       return g_strdup (priv->format);
+}
+
+/**
+ * zak_form_element_validator_date_xml_parsing:
+ * @validator:
+ * @xnode:
+ *
+ */
+gboolean
+zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode)
+{
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator));
+
+       zak_form_element_validator_date_set_format (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator), (gchar *)xmlNodeGetContent (xnode));
+
+       return TRUE;
+}
+
+/* PRIVATE */
+static void
+zak_form_element_validator_date_set_property (GObject *object,
+                   guint property_id,
+                   const GValue *value,
+                   GParamSpec *pspec)
+{
+       ZakFormElementValidatorDate *zak_form_element = (ZakFormElementValidatorDate *)object;
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_form_element_validator_date_get_property (GObject *object,
+                   guint property_id,
+                   GValue *value,
+                   GParamSpec *pspec)
+{
+       ZakFormElementValidatorDate *zak_form_element = (ZakFormElementValidatorDate *)object;
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+zak_form_element_validator_date_dispose (GObject *gobject)
+{
+       ZakFormElementValidatorDate *zak_form_element = (ZakFormElementValidatorDate *)gobject;
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element);
+
+
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->dispose (gobject);
+}
+
+static void
+zak_form_element_validator_date_finalize (GObject *gobject)
+{
+       ZakFormElementValidatorDate *zak_form_element = (ZakFormElementValidatorDate *)gobject;
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (zak_form_element);
+
+       g_free (priv->format);
+
+       GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+       parent_class->finalize (gobject);
+}
+
+static gboolean
+zak_form_element_validator_date_validate (ZakFormElementValidator *validator_date,
+                                                                                 const gchar *value)
+{
+       gboolean ret;
+       gchar *_value;
+       GDateTime *gdt;
+
+       ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator_date);
+
+       g_return_val_if_fail (value != NULL, FALSE);
+
+       _value = g_strstrip (g_strdup (value));
+       if (g_strcmp0 (_value, "") == 0)
+               {
+                       ret = TRUE;
+               }
+       else
+               {
+                       gdt = zak_utils_get_gdatetime_from_string (_value, priv->format);
+                       ret = (gdt != NULL);
+
+                       if (gdt != NULL)
+                               {
+                                       g_date_time_unref (gdt);
+                               }
+               }
+       g_free (_value);
+
+       return ret;
+}
diff --git a/src/formelementvalidatordate.h b/src/formelementvalidatordate.h
new file mode 100644 (file)
index 0000000..accea52
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2015 Andrea Zagli <azagli@libero.it>
+ *
+ * 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_FORM_ELEMENT_VALIDATOR_DATE_H__
+#define __ZAK_FORM_ELEMENT_VALIDATOR_DATE_H__
+
+
+#include <glib-object.h>
+
+#include <libxml/tree.h>
+
+#include "formelementvalidator.h"
+
+
+G_BEGIN_DECLS
+
+
+#define ZAK_TYPE_FORM_ELEMENT_VALIDATOR_DATE zak_form_element_validator_date_get_type ()
+G_DECLARE_FINAL_TYPE (ZakFormElementValidatorDate, zak_form_element_validator_date, ZAK_FORM, ELEMENT_VALIDATOR_DATE, ZakFormElementValidator)
+
+ZakFormElementValidatorDate *zak_form_element_validator_date_new (void);
+
+void zak_form_element_validator_date_set_format (ZakFormElementValidatorDate *validator, const gchar *format);
+gchar *zak_form_element_validator_date_get_format (ZakFormElementValidatorDate *validator);
+
+gboolean zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode);
+
+
+G_END_DECLS
+
+
+#endif /* __ZAK_FORM_ELEMENT_VALIDATOR_DATE_H__ */
index 6279769f25fab3991010b7b73a9d1df5d9ee8bf3..17db5ebd551e27677fa1d6bbe7377dd8a8012ec9 100644 (file)
@@ -120,7 +120,6 @@ zak_form_element_validator_regex_xml_parsing (ZakFormElementValidator *validator
                        g_free (priv->regex);
                }
        priv->regex = g_strdup ((gchar *)xmlNodeGetContent (xnode));
-       g_message ("regex: %s", priv->regex);
 
        return TRUE;
 }