]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added function FormElement::add_message.
authorAndrea Zagli <azagli@libero.it>
Wed, 6 Jan 2021 08:59:59 +0000 (09:59 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 6 Jan 2021 08:59:59 +0000 (09:59 +0100)
src/formelement.c
src/formelement.h

index c4364f211d69a9c4b3122535f50c9030b03a41ae..c3b60530bbcf7b91f3581a2a5b9b0479b0f61066 100644 (file)
@@ -1369,7 +1369,7 @@ zak_form_element_is_valid (ZakFormElement *element)
                                        ZakFormElementValidator *validator = (ZakFormElementValidator *)g_ptr_array_index (priv->pa_validators, i);
                                        if (!zak_form_element_validator_validate (validator, value))
                                                {
-                                                       g_ptr_array_add (priv->pa_messages, (gpointer)g_strdup (zak_form_element_validator_get_message (validator)));
+                                                       zak_form_element_add_message (element, (const gchar *)g_strdup (zak_form_element_validator_get_message (validator)));
 
                                                        ret = FALSE;
                                                }
@@ -1441,12 +1441,52 @@ GPtrArray
 void
 zak_form_element_set_messages (ZakFormElement *element, GPtrArray *messages)
 {
+       guint i;
+
        ZakFormElementPrivate *priv = zak_form_element_get_instance_private (element);
 
        if (priv->pa_messages != NULL)
                {
                        g_ptr_array_free (priv->pa_messages, TRUE);
-                       priv->pa_messages = copy_ar_func (messages);
+               }
+
+       if (messages == NULL)
+               {
+                       return;
+               }
+
+       priv->pa_messages = g_ptr_array_new ();
+
+       for (i = 0; i < messages->len; i++)
+               {
+                       zak_form_element_add_message (element, (const gchar *)g_strdup ((gchar *)g_ptr_array_index (messages, i)));
+               }
+}
+
+/**
+ * zak_form_element_add_message:
+ * @element:
+ * @message:
+ *
+ * Add an element's message.
+ */
+void
+zak_form_element_add_message (ZakFormElement *element, const gchar *message)
+{
+       ZakFormElementPrivate *priv = zak_form_element_get_instance_private (element);
+
+       if (priv->pa_messages == NULL)
+               {
+                       priv->pa_messages = g_ptr_array_new ();
+               }
+
+       /* check duplicate */
+       if (!g_ptr_array_find_with_equal_func (priv->pa_messages,
+                                              message,
+                                              g_str_equal,
+                                              NULL))
+               {
+                       g_ptr_array_add (priv->pa_messages, (gpointer)g_strdup (message));
                }
 }
 
index 6d31e7b053d96af1e5bc9f10af051b08103ba8be..152b10d9fb49d4c724860437cb60ce07ea97148d 100644 (file)
@@ -137,6 +137,7 @@ void zak_form_element_add_validator (ZakFormElement *element, ZakFormElementVali
 gboolean zak_form_element_is_valid (ZakFormElement *element);
 GPtrArray *zak_form_element_get_messages (ZakFormElement *element);
 void zak_form_element_set_messages (ZakFormElement *element, GPtrArray *messages);
+void zak_form_element_add_message (ZakFormElement *element, const gchar *message);
 
 GPtrArray *zak_form_element_get_validators (ZakFormElement *element);
 ZakFormElementValidator *zak_form_element_get_validator_by_id (ZakFormElement *element, const gchar *id);