]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added element for error message to FormValidatorCompare.
authorAndrea Zagli <azagli@libero.it>
Wed, 6 Jan 2021 09:22:08 +0000 (10:22 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 6 Jan 2021 09:22:08 +0000 (10:22 +0100)
src/formvalidatorcompare.c
src/formvalidatorcompare.h

index 64b93cd6180efc09eac836a89572e713cf5bcd8c..208819bf4ca448133208dd68a4b4653a9a1cff6c 100644 (file)
@@ -61,6 +61,8 @@ struct _ZakFormValidatorComparePrivate
 
                ZakFormElement *v1;
                ZakFormElement *v2;
+
+               ZakFormElement *message;
        };
 
 G_DEFINE_TYPE_WITH_PRIVATE (ZakFormValidatorCompare, zak_form_validator_compare, ZAK_FORM_TYPE_VALIDATOR)
@@ -88,6 +90,7 @@ zak_form_validator_compare_init (ZakFormValidatorCompare *validator)
 
        priv->v1 = NULL;
        priv->v2 = NULL;
+       priv->message = NULL;
 }
 
 /**
@@ -131,6 +134,10 @@ zak_form_validator_compare_xml_parsing (ZakFormValidator *validator, xmlNode *xn
        zak_form_validator_compare_set_element2 ((ZakFormValidatorCompare *)validator, (ZakFormForm *)form, prop);
        g_free (prop);
 
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"element_message");
+       zak_form_validator_compare_set_element_message ((ZakFormValidatorCompare *)validator, (ZakFormForm *)form, prop);
+       g_free (prop);
+
        return TRUE;
 }
 
@@ -243,6 +250,46 @@ const gchar
                }
 }
 
+/**
+ * zak_form_validator_compare_set_element_message:
+ * @validator:
+ * @element:
+ *
+ * Sets the validator element with error message.
+ */
+void
+zak_form_validator_compare_set_element_message (ZakFormValidatorCompare *validator, ZakFormForm *form, const gchar *element)
+{
+       ZakFormValidatorComparePrivate *priv = zak_form_validator_compare_get_instance_private (validator);
+
+       priv->message = zak_form_form_get_element_by_id (form, element);
+       if (!ZAK_FORM_IS_ELEMENT (priv->message))
+               {
+                       g_warning ("Validator compare: element «%s» isn't a ZakFormElement.", element);
+               }
+}
+
+/**
+ * zak_form_validator_compare_get_element_message:
+ * @validator:
+ *
+ * Returns: the validator element name with error message.
+ */
+const gchar
+*zak_form_validator_compare_get_element_message (ZakFormValidatorCompare *validator)
+{
+       ZakFormValidatorComparePrivate *priv = zak_form_validator_compare_get_instance_private (validator);
+
+       if (ZAK_FORM_IS_ELEMENT (priv->message))
+               {
+                       return zak_form_element_get_name (priv->message);
+               }
+       else
+               {
+                       return "";
+               }
+}
+
 /* PRIVATE */
 static void
 zak_form_validator_compare_set_property (GObject *object,
@@ -352,6 +399,12 @@ zak_form_validator_compare_validate (ZakFormValidator *validator)
                                                               zak_form_get_compare_type_stringify (priv->type),
                                                               zak_form_element_get_long_name (priv->v2));
                                        zak_form_validator_set_message (validator, msg);
+
+                                       if (priv->message != NULL)
+                                               {
+                                                       zak_form_element_add_message (priv->message, msg);
+                                               }
+
                                        g_free (msg);
                                }
                }
@@ -372,4 +425,6 @@ zak_form_validator_compare_get_xml (ZakFormValidator *validator, xmlNode *xnode,
        xmlSetProp (xnode, (const xmlChar *)"element1", (const xmlChar *)zak_form_validator_compare_get_element1 (validator_compare));
 
        xmlSetProp (xnode, (const xmlChar *)"element2", (const xmlChar *)zak_form_validator_compare_get_element2 (validator_compare));
+
+       xmlSetProp (xnode, (const xmlChar *)"element_message", (const xmlChar *)zak_form_validator_compare_get_element_message (validator_compare));
 }
index 845d2f5e3a39bc76956ff3e646200f81906ada06..05b739c6131580e7b66184257699797519abc511 100644 (file)
@@ -45,6 +45,9 @@ const gchar *zak_form_validator_compare_get_element1 (ZakFormValidatorCompare *v
 void zak_form_validator_compare_set_element2 (ZakFormValidatorCompare *validator, ZakFormForm *form, const gchar *element2);
 const gchar *zak_form_validator_compare_get_element2 (ZakFormValidatorCompare *validator);
 
+void zak_form_validator_compare_set_element_message (ZakFormValidatorCompare *validator, ZakFormForm *form, const gchar *element);
+const gchar *zak_form_validator_compare_get_element_message (ZakFormValidatorCompare *validator);
+
 G_END_DECLS