formelementvalidatordate.c \
formelementvalidatornotempty.c \
formelementvalidatorregex.c \
- formiprovider.c
+ formiprovider.c \
+ formvalidator.c
libzakform_la_LDFLAGS = -no-undefined
formelementvalidatordate.h \
formelementvalidatornotempty.h \
formelementvalidatorregex.h \
- formiprovider.h
+ formiprovider.h \
+ formvalidator.h
libzakform_includedir = $(includedir)/libzakform
#include <libxml/xpath.h>
#include "form.h"
-#include "formelement.h"
#include "formelementfilter.h"
#include "formelementvalidator.h"
typedef gboolean (* FormElementFilterXmlParsingFunc) (ZakFormElementFilter *, xmlNodePtr);
typedef ZakFormElementValidator *(* FormElementValidatorConstructorFunc) (void);
typedef gboolean (* FormElementValidatorXmlParsingFunc) (ZakFormElementValidator *, xmlNodePtr);
+typedef ZakFormValidator *(* FormValidatorConstructorFunc) (void);
+typedef gboolean (* FormValidatorXmlParsingFunc) (ZakFormValidator *, xmlNodePtr);
static void zak_form_form_class_init (ZakFormFormClass *class);
static void zak_form_form_init (ZakFormForm *zak_form_form);
{
GPtrArray *ar_modules;
GPtrArray *ar_elements;
+ GPtrArray *ar_validators;
} ZakFormFormPrivate;
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormForm, zak_form_form, G_TYPE_OBJECT)
priv->ar_modules = NULL;
priv->ar_elements = g_ptr_array_new ();
+ priv->ar_validators = g_ptr_array_new ();
#ifdef G_OS_WIN32
gboolean ret;
ZakFormElement *element;
+ ZakFormValidator *validator;
+
gchar *type;
guint i;
FormElementConstructorFunc element_constructor;
FormElementXmlParsingFunc element_xml_parsing;
+ FormValidatorConstructorFunc validator_constructor;
+ FormValidatorXmlParsingFunc validator_xml_parsing;
+
g_return_val_if_fail (ZAK_FORM_IS_FORM (zakform), FALSE);
g_return_val_if_fail (xmldoc != NULL, FALSE);
g_warning (_("Unknown element type «%s»."), type);
}
}
+ else if (xmlStrcmp (cur->name, (const xmlChar *)"validator") == 0)
+ {
+ type = xmlGetProp (cur, (const xmlChar *)"type");
+
+ /* for each module */
+ for (i = 0; i < priv->ar_modules->len; i++)
+ {
+ if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i),
+ g_strconcat (type, "_new", NULL),
+ (gpointer *)&validator_constructor))
+ {
+ if (validator_constructor != NULL)
+ {
+ validator = validator_constructor ();
+ zak_form_form_add_validator (zakform, validator);
+
+ if (g_module_symbol ((GModule *)g_ptr_array_index (priv->ar_modules, i),
+ g_strconcat (type, "_xml_parsing", NULL),
+ (gpointer *)&validator_xml_parsing))
+ {
+ if (validator_xml_parsing != NULL)
+ {
+ validator_xml_parsing (validator, cur);
+ }
+ }
+
+ break;
+ }
+ }
+ }
+ if (i >= priv->ar_modules->len)
+ {
+ g_warning ("Validator «%s» not found.", type);
+ }
+ }
cur = cur->next;
}
return ret;
}
+/**
+ * zak_form_form_get_element_by_id:
+ * @zakform:
+ * @id:
+ *
+ * Returns: the #ZakFormElement with @id.
+ */
+ZakFormElement
+*zak_form_form_get_element_by_id (ZakFormForm *zakform, const gchar *id)
+{
+ ZakFormElement *ret;
+
+ ZakFormFormPrivate *priv;
+
+ guint i;
+
+ priv = zak_form_form_get_instance_private (zakform);
+
+ ret = NULL;
+ for (i = 0; i < priv->ar_elements->len; i++)
+ {
+ ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (priv->ar_elements, i);
+
+ if (g_strcmp0 (zak_form_element_get_name (element), id) == 0)
+ {
+ ret = element;
+ break;
+ }
+ }
+
+ return ret;
+}
+
+/**
+ * zak_form_form_add_validator:
+ * @zakform:
+ * @validator:
+ *
+ * Returns: #TRUE if @validator is added; FALSE otherwise.
+ */
+gboolean
+zak_form_form_add_validator (ZakFormForm *zakform, ZakFormValidator *validator)
+{
+ gboolean ret;
+
+ ZakFormFormPrivate *priv;
+
+ priv = zak_form_form_get_instance_private (zakform);
+
+ g_ptr_array_add (priv->ar_validators, g_object_ref (validator));
+ ret = TRUE;
+
+ return ret;
+}
+
/**
* zak_form_form_clear:
* @zakform: a #ZakFormForm object.
#include "formelement.h"
#include "formiprovider.h"
+#include "formvalidator.h"
G_BEGIN_DECLS
gboolean zak_form_form_load_from_file (ZakFormForm *zakform, const gchar *filename);
gboolean zak_form_form_add_element (ZakFormForm *zakform, ZakFormElement *element);
+ZakFormElement *zak_form_form_get_element_by_id (ZakFormForm *zakform, const gchar *id);
+
+gboolean zak_form_form_add_validator (ZakFormForm *zakform, ZakFormValidator *validator);
void zak_form_form_clear (ZakFormForm *zakform);
--- /dev/null
+/*
+ * 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 "formvalidator.h"
+
+enum
+ {
+ PROP_0,
+ PROP_MESSAGE
+ };
+
+static void zak_form_validator_class_init (ZakFormValidatorClass *class);
+static void zak_form_validator_init (ZakFormValidator *zak_form_validator);
+
+static void zak_form_validator_set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *pspec);
+static void zak_form_validator_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec);
+
+static void zak_form_validator_dispose (GObject *gobject);
+static void zak_form_validator_finalize (GObject *gobject);
+
+typedef struct
+ {
+ gchar *message;
+ } ZakFormValidatorPrivate;
+
+G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormValidator, zak_form_validator, G_TYPE_OBJECT)
+
+static void
+zak_form_validator_class_init (ZakFormValidatorClass *class)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+ object_class->set_property = zak_form_validator_set_property;
+ object_class->get_property = zak_form_validator_get_property;
+ object_class->dispose = zak_form_validator_dispose;
+ object_class->finalize = zak_form_validator_finalize;
+
+ g_object_class_install_property (object_class, PROP_MESSAGE,
+ g_param_spec_string ("message",
+ "Message",
+ "Message",
+ "Invalid value",
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+}
+
+static void
+zak_form_validator_init (ZakFormValidator *zak_form_validator)
+{
+ ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator);
+}
+
+gboolean
+zak_form_validator_validate (ZakFormValidator *self, GPtrArray *ar_elements)
+{
+ gboolean ret;
+
+ g_return_val_if_fail (ZAK_FORM_IS_VALIDATOR (self), FALSE);
+
+ ret = TRUE;
+ if (ZAK_FORM_VALIDATOR_GET_CLASS (self)->validate != NULL)
+ {
+ ret = ZAK_FORM_VALIDATOR_GET_CLASS (self)->validate (self, ar_elements);
+ }
+ return ret;
+}
+
+/**
+ * zak_form_validator_set_message:
+ * @validator:
+ * @message:
+ *
+ */
+void
+zak_form_validator_set_message (ZakFormValidator *validator,
+ const gchar *message)
+{
+ ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator);
+
+ priv->message = g_strdup (message);
+}
+
+/**
+ * zak_form_validator_get_message:
+ * @validator:
+ *
+ * Returns:
+ */
+gchar
+*zak_form_validator_get_message (ZakFormValidator *validator)
+{
+ ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (validator);
+
+ return g_strdup (priv->message);
+}
+
+/* PRIVATE */
+static void
+zak_form_validator_set_property (GObject *object,
+ guint property_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ ZakFormValidator *zak_form_validator = (ZakFormValidator *)object;
+ ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator);
+
+ switch (property_id)
+ {
+ case PROP_MESSAGE:
+ zak_form_validator_set_message (zak_form_validator, g_value_dup_string (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+zak_form_validator_get_property (GObject *object,
+ guint property_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ ZakFormValidator *zak_form_validator = (ZakFormValidator *)object;
+ ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator);
+
+ switch (property_id)
+ {
+ case PROP_MESSAGE:
+ g_value_set_string (value, zak_form_validator_get_message (zak_form_validator));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+ break;
+ }
+}
+
+static void
+zak_form_validator_dispose (GObject *gobject)
+{
+ ZakFormValidator *zak_form_validator = (ZakFormValidator *)gobject;
+ ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator);
+
+
+
+ GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+ parent_class->dispose (gobject);
+}
+
+static void
+zak_form_validator_finalize (GObject *gobject)
+{
+ ZakFormValidator *zak_form_validator = (ZakFormValidator *)gobject;
+ ZakFormValidatorPrivate *priv = zak_form_validator_get_instance_private (zak_form_validator);
+
+
+
+ GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
+ parent_class->finalize (gobject);
+}
--- /dev/null
+/*
+ * 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_VALIDATOR_H_
+#define _ZAK_FORM_VALIDATOR_H_
+
+#include <glib-object.h>
+
+
+G_BEGIN_DECLS
+
+
+#define ZAK_FORM_TYPE_VALIDATOR zak_form_validator_get_type ()
+G_DECLARE_DERIVABLE_TYPE (ZakFormValidator, zak_form_validator, ZAK_FORM, VALIDATOR, GObject)
+
+struct _ZakFormValidatorClass
+{
+ GObjectClass parent_class;
+
+ gboolean (*validate) (ZakFormValidator *self, GPtrArray *ar_elements);
+};
+
+gboolean zak_form_validator_validate (ZakFormValidator *self, GPtrArray *ar_elements);
+
+void zak_form_validator_set_message (ZakFormValidator *validator,
+ const gchar *message);
+gchar *zak_form_validator_get_message (ZakFormValidator *validator);
+
+
+G_END_DECLS
+
+
+#endif /* _ZAK_FORM_VALIDATOR_H_ */
#include <libzakform/formiprovider.h>
+#include <libzakform/formvalidator.h>
+
#endif /* __LIBZAKFORM_H__ */