]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added property message to ZakFormElementValidator.
authorAndrea Zagli <azagli@libero.it>
Sun, 1 Nov 2015 16:11:35 +0000 (17:11 +0100)
committerAndrea Zagli <azagli@libero.it>
Sun, 1 Nov 2015 16:12:18 +0000 (17:12 +0100)
src/formelement.c
src/formelement.h
src/formelementvalidator.c
src/formelementvalidator.h
src/formelementvalidatornotempty.h
src/formelementvalidatorregex.h

index 53e9785a1d64298d464810884df1116dac1cf409..5b31dcff61db2ca0868faa86ae7c7b4e1eeb1111 100644 (file)
@@ -46,11 +46,14 @@ static void zak_form_element_get_property (GObject *object,
 static void zak_form_element_dispose (GObject *gobject);
 static void zak_form_element_finalize (GObject *gobject);
 
+static GPtrArray *zak_form_element_get_messages (ZakFormElement *element);
+
 typedef struct
        {
                gchar *value;
                GPtrArray *pa_filters;
                GPtrArray *pa_validators;
+               GPtrArray *pa_messages;
        } ZakFormElementPrivate;
 
 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormElement, zak_form_element, G_TYPE_OBJECT)
@@ -64,6 +67,8 @@ zak_form_element_class_init (ZakFormElementClass *class)
        object_class->get_property = zak_form_element_get_property;
        object_class->dispose = zak_form_element_dispose;
        object_class->finalize = zak_form_element_finalize;
+
+       class->get_messages = zak_form_element_get_messages;
 }
 
 static void
@@ -74,6 +79,7 @@ zak_form_element_init (ZakFormElement *zak_form_element)
        priv->value = NULL;
        priv->pa_filters = NULL;
        priv->pa_validators = NULL;
+       priv->pa_messages = NULL;
 }
 
 /**
@@ -202,15 +208,27 @@ zak_form_element_is_valid (ZakFormElement *element)
 
        zak_form_element_filter (element);
 
+       if (priv->pa_messages != NULL)
+               {
+                       g_ptr_array_unref (priv->pa_messages);
+                       priv->pa_messages = NULL;
+               }
+
        if (priv->pa_validators != NULL)
                {
                        value = zak_form_element_get_value (element);
 
                        for (i = 0; i < priv->pa_validators->len; i++)
                                {
-                                       if (!zak_form_element_validator_validate ((ZakFormElementValidator *)g_ptr_array_index (priv->pa_validators, i),
-                                                                                                                                  value))
+                                       ZakFormElementValidator *validator = (ZakFormElementValidator *)g_ptr_array_index (priv->pa_validators, i);
+                                       if (!zak_form_element_validator_validate (validator, value))
                                                {
+                                                       if (priv->pa_messages == NULL)
+                                                               {
+                                                                       priv->pa_messages = g_ptr_array_new ();
+                                                               }
+                                                       g_ptr_array_add (priv->pa_messages, (gpointer)g_strdup (zak_form_element_validator_get_message (validator)));
+
                                                        ret = FALSE;
                                                }
                                }
@@ -277,3 +295,11 @@ zak_form_element_finalize (GObject *gobject)
        GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject));
        parent_class->finalize (gobject);
 }
+
+static GPtrArray
+*zak_form_element_get_messages (ZakFormElement *element)
+{
+       ZakFormElementPrivate *priv = zak_form_element_get_instance_private (element);
+
+       return priv->pa_messages;
+}
index cb3594c3bb0afcd35e982802996665fa33604770..ffe0ff10dbb576d8b4a2973d1ee84fc447efc1bc 100644 (file)
@@ -35,6 +35,8 @@ G_DECLARE_DERIVABLE_TYPE (ZakFormElement, zak_form_element, ZAK_FORM, ELEMENT, G
 struct _ZakFormElementClass
        {
                GObjectClass parent_class;
+
+               GPtrArray *(*get_messages) (ZakFormElement *element);
        };
 
 
index e2c9b0e07ce2951f4f83ffe9fd228da0fdcdc100..00705b48f63d62a3a721888ff76f0bfb5a2f989d 100644 (file)
 
 #include "formelementvalidator.h"
 
+enum
+       {
+               PROP_0,
+               PROP_MESSAGE
+       };
+
 static void zak_form_element_validator_class_init (ZakFormElementValidatorClass *class);
 static void zak_form_element_validator_init (ZakFormElementValidator *zak_form_element_validator);
 
@@ -39,7 +45,7 @@ static void zak_form_element_validator_finalize (GObject *gobject);
 
 typedef struct
        {
-           gpointer nothing;
+           gchar *message;
        } ZakFormElementValidatorPrivate;
 
 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ZakFormElementValidator, zak_form_element_validator, G_TYPE_OBJECT)
@@ -53,6 +59,13 @@ zak_form_element_validator_class_init (ZakFormElementValidatorClass *class)
        object_class->get_property = zak_form_element_validator_get_property;
        object_class->dispose = zak_form_element_validator_dispose;
        object_class->finalize = zak_form_element_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
@@ -76,6 +89,35 @@ zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar
        return ret;
 }
 
+/**
+ * zak_form_element_validator_set_message:
+ * @validator:
+ * @message:
+ *
+ */
+void
+zak_form_element_validator_set_message (ZakFormElementValidator *validator,
+                                                                               const gchar *message)
+{
+       ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator);
+
+       priv->message = g_strdup (message);
+}
+
+/**
+ * zak_form_element_validator_get_message:
+ * @validator:
+ *
+ * Returns:
+ */
+gchar
+*zak_form_element_validator_get_message (ZakFormElementValidator *validator)
+{
+       ZakFormElementValidatorPrivate *priv = zak_form_element_validator_get_instance_private (validator);
+
+    return g_strdup (priv->message);
+}
+
 /* PRIVATE */
 static void
 zak_form_element_validator_set_property (GObject *object,
@@ -88,9 +130,13 @@ zak_form_element_validator_set_property (GObject *object,
 
        switch (property_id)
                {
-                       default:
-                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-                               break;
+               case PROP_MESSAGE:
+                   zak_form_element_validator_set_message (zak_form_element_validator, g_value_dup_string (value));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
                }
 }
 
@@ -105,9 +151,13 @@ zak_form_element_validator_get_property (GObject *object,
 
        switch (property_id)
                {
-                       default:
-                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-                               break;
+               case PROP_MESSAGE:
+                       g_value_set_string (value, zak_form_element_validator_get_message (zak_form_element_validator));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                       break;
                }
 }
 
index b1b8145ae4c85792dcbeda3a8d50e35219ff0678..06ab451969af0d9638333d711b380666773d5529 100644 (file)
@@ -37,6 +37,10 @@ struct _ZakFormElementValidatorClass
 
 gboolean zak_form_element_validator_validate (ZakFormElementValidator *self, const gchar *value);
 
+void zak_form_element_validator_set_message (ZakFormElementValidator *validator,
+                                                                                        const gchar *message);
+gchar *zak_form_element_validator_get_message (ZakFormElementValidator *validator);
+
 
 G_END_DECLS
 
index 4ee42e1c951c019e1d0b938ad32b9cc150043642..8d82bf709d166dfb23025a1f8ba8a773b6b948db 100644 (file)
@@ -22,7 +22,7 @@
 
 #include <glib-object.h>
 
-#include <formelementvalidator.h>
+#include "formelementvalidator.h"
 
 
 G_BEGIN_DECLS
index 7add4298c8d951d299e49b6b8863ae831309c936..627deab62e779f896e3775fa2de189b0aa12cbbc 100644 (file)
@@ -22,7 +22,7 @@
 
 #include <glib-object.h>
 
-#include <formelementvalidator.h>
+#include "formelementvalidator.h"
 
 
 G_BEGIN_DECLS