]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Added property error on elements for FormValidatorComposite.
authorAndrea Zagli <azagli@libero.it>
Wed, 6 Jan 2021 11:58:14 +0000 (12:58 +0100)
committerAndrea Zagli <azagli@libero.it>
Wed, 6 Jan 2021 11:58:14 +0000 (12:58 +0100)
src/formvalidatorcomposite.c
src/formvalidatorcomposite.h

index e4a13d81234878b85aa9510cc1182c69320a4c88..fd7b2dfe78040b4681c3b6fa004d0f48c05bf8c5 100644 (file)
@@ -60,6 +60,7 @@ typedef struct _ZakFormValidatorCompositePrivate ZakFormValidatorCompositePrivat
 struct _ZakFormValidatorCompositePrivate
        {
                GNode *tree;
+               gboolean set_error;
        };
 
 G_DEFINE_TYPE_WITH_PRIVATE (ZakFormValidatorComposite, zak_form_validator_composite, ZAK_FORM_TYPE_VALIDATOR)
@@ -79,6 +80,7 @@ typedef struct
                ZakFormElementValidator *element_validator;
                ZakFormElement *element;
                gchar *message;
+               gboolean valid;
        } Node;
 
 static void
@@ -102,6 +104,7 @@ zak_form_validator_composite_init (ZakFormValidatorComposite *validator)
        ZakFormValidatorCompositePrivate *priv = zak_form_validator_composite_get_instance_private (validator);
 
        priv->tree = NULL;
+       priv->set_error = FALSE;
 }
 
 /**
@@ -119,6 +122,23 @@ ZakFormValidatorComposite
        return zak_form_validator_composite;
 }
 
+void
+zak_form_validator_composite_set_error_on_elements (ZakFormValidatorComposite *validator, gboolean set_error)
+{
+       ZakFormValidatorCompositePrivate *priv = zak_form_validator_composite_get_instance_private (validator);
+
+       priv->set_error = set_error;
+}
+
+gboolean
+zak_form_validator_composite_get_error_on_elements (ZakFormValidatorComposite *validator)
+{
+       ZakFormValidatorCompositePrivate *priv = zak_form_validator_composite_get_instance_private (validator);
+
+       return priv->set_error;
+}
+
+
 static void
 _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, ZakFormForm *form, GNode *tree)
 {
@@ -136,6 +156,7 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                                        Node *n;
 
                                        n = (Node *)g_new0 (Node, 1);
+                                       n->valid = TRUE;
 
                                        n->condition = TRUE;
 
@@ -211,6 +232,7 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                                        Node *n;
 
                                        n = (Node *)g_new0 (Node, 1);
+                                       n->valid = TRUE;
 
                                        if (xmlStrEqual (xmlGetProp (cur, (xmlChar *)"type"), (xmlChar *)"and"))
                                                {
@@ -248,6 +270,7 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                                        Node *n;
 
                                        n = (Node *)g_new0 (Node, 1);
+                                       n->valid = TRUE;
 
                                        if (g_strcmp0 ((gchar *)xmlGetProp (cur, (xmlChar *)"validator_type"), "form") == 0)
                                                {
@@ -283,6 +306,7 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                                                                }
 
                                                        n = (Node *)g_new0 (Node, 1);
+                                                       n->valid = TRUE;
 
                                                        validator_constructor = zak_form_get_form_element_validator ((gchar *)xmlGetProp (cur, (xmlChar *)"type"));
                                                        if (validator_constructor == NULL)
@@ -323,9 +347,14 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
 static gboolean
 zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode *xnode, gpointer form)
 {
+       gchar *prop;
 
        ZakFormValidatorCompositePrivate *priv = zak_form_validator_composite_get_instance_private (ZAK_FORM_VALIDATOR_COMPOSITE (validator));
 
+       prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"error_on_elements");
+       zak_form_validator_composite_set_error_on_elements ((ZakFormValidatorComposite *)validator, zak_utils_string_to_boolean (prop));
+       g_free (prop);
+
        _zak_form_validator_composite_xml_parsing (validator, xnode, (ZakFormForm *)form, NULL);
 
        return TRUE;
@@ -427,14 +456,15 @@ _zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *pare
                                                {
                                                        if (zak_form_validator_validate (n_child->form_validator) == n_child->condition_type)
                                                                {
+                                                                       n_child->valid = _zak_form_validator_composite_validate (validator, child);
                                                                        switch (n->logic_type)
                                                                                {
                                                                                case LOGIC_TYPE_AND:
-                                                                                       ret = (ret && _zak_form_validator_composite_validate (validator, child));
+                                                                                       ret = (ret && n_child->valid);
                                                                                        break;
 
                                                                                case LOGIC_TYPE_OR:
-                                                                                       ret = (ret || _zak_form_validator_composite_validate (validator, child));
+                                                                                       ret = (ret || n_child->valid);
                                                                                        break;
                                                                                }
                                                                }
@@ -444,14 +474,15 @@ _zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *pare
                                                        if (zak_form_element_validator_validate (n_child->element_validator,
                                                                                                 zak_form_element_get_value_gvalue (n_child->element)) == n_child->condition_type)
                                                                {
+                                                                       n_child->valid = _zak_form_validator_composite_validate (validator, child);
                                                                        switch (n->logic_type)
                                                                                {
                                                                                case LOGIC_TYPE_AND:
-                                                                                       ret = (ret && _zak_form_validator_composite_validate (validator, child));
+                                                                                       ret = (ret && n_child->valid);
                                                                                        break;
 
                                                                                case LOGIC_TYPE_OR:
-                                                                                       ret = (ret || _zak_form_validator_composite_validate (validator, child));
+                                                                                       ret = (ret || n_child->valid);
                                                                                        break;
                                                                                }
                                                                }
@@ -464,18 +495,21 @@ _zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *pare
                                                case LOGIC_TYPE_AND:
                                                        if (n_child->logic_type > 0)
                                                                {
-                                                                       ret = (ret && _zak_form_validator_composite_validate (validator, child));
+                                                                       n_child->valid = _zak_form_validator_composite_validate (validator, child);
+                                                                       ret = (ret && n_child->valid);
                                                                }
                                                        else
                                                                {
                                                                        if (n_child->form_validator != NULL)
                                                                                {
-                                                                                       ret = (ret && zak_form_validator_validate (n_child->form_validator));
+                                                                                       n_child->valid = zak_form_validator_validate (n_child->form_validator);
+                                                                                       ret = (ret && n_child->valid);
                                                                                }
                                                                        else
                                                                                {
-                                                                                       ret = (ret && zak_form_element_validator_validate (n_child->element_validator,
-                                                                                                                                          zak_form_element_get_value_gvalue (n_child->element)));
+                                                                                       n_child->valid = zak_form_element_validator_validate (n_child->element_validator,
+                                                                                                                                             zak_form_element_get_value_gvalue (n_child->element));
+                                                                                       ret = (ret && n_child->valid);
                                                                                }
                                                                }
                                                        break;
@@ -483,18 +517,21 @@ _zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *pare
                                                case LOGIC_TYPE_OR:
                                                        if (n_child->logic_type > 0)
                                                                {
-                                                                       ret = (ret || _zak_form_validator_composite_validate (validator, child));
+                                                                       n_child->valid = _zak_form_validator_composite_validate (validator, child);
+                                                                       ret = (ret || n_child->valid);
                                                                }
                                                        else
                                                                {
                                                                        if (n_child->form_validator != NULL)
                                                                                {
-                                                                                       ret = (ret || zak_form_validator_validate (n_child->form_validator));
+                                                                                       n_child->valid = zak_form_validator_validate (n_child->form_validator);
+                                                                                       ret = (ret || n_child->valid);
                                                                                }
                                                                        else
                                                                                {
-                                                                                       ret = (ret || zak_form_element_validator_validate (n_child->element_validator,
-                                                                                                                                          zak_form_element_get_value_gvalue (n_child->element)));
+                                                                                       n_child->valid = zak_form_element_validator_validate (n_child->element_validator,
+                                                                                                                                             zak_form_element_get_value_gvalue (n_child->element));
+                                                                                       ret = (ret || n_child->valid);
                                                                                }
                                                                }
                                                        break;
@@ -522,6 +559,34 @@ _zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *pare
        return ret;
 }
 
+static gboolean
+_zak_form_validator_composite_set_elements_message (ZakFormValidator *validator, GNode *parent)
+{
+       guint i;
+       guint children;
+
+       GNode *child;
+       Node *n_child;
+
+       children = g_node_n_children (parent);
+       for (i = 0; i < children; i++)
+               {
+                       child = g_node_nth_child (parent, i);
+                       n_child = (Node *)child->data;
+
+                       if (!n_child->valid)
+                               {
+                                       if (n_child->form_validator == NULL
+                                           && n_child->element_validator != NULL
+                                           && n_child->element != NULL)
+                                               {
+                                                       zak_form_element_add_message (n_child->element, zak_form_element_validator_get_message (n_child->element_validator));
+                                               }
+                                       _zak_form_validator_composite_set_elements_message (validator, child);
+                               }
+               }
+}
+
 static gboolean
 zak_form_validator_composite_validate (ZakFormValidator *validator)
 {
@@ -532,6 +597,11 @@ zak_form_validator_composite_validate (ZakFormValidator *validator)
        if (priv->tree != NULL)
                {
                        ret = _zak_form_validator_composite_validate (validator, g_node_get_root (priv->tree));
+                       if (!ret
+                           && priv->set_error)
+                               {
+                                       _zak_form_validator_composite_set_elements_message (validator, g_node_get_root (priv->tree));
+                               }
                }
        else
                {
index e3bd8703e657925daa630fee1853222424a4f1bc..1018ffd0272e7c25e654f589b2654745c8ef86be 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2017-2020 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
@@ -36,6 +36,9 @@ G_DECLARE_FINAL_TYPE (ZakFormValidatorComposite, zak_form_validator_composite, Z
 
 ZakFormValidatorComposite *zak_form_validator_composite_new (void);
 
+void zak_form_validator_composite_set_error_on_elements (ZakFormValidatorComposite *validator, gboolean set_error);
+gboolean zak_form_validator_composite_get_error_on_elements (ZakFormValidatorComposite *validator);
+
 
 G_END_DECLS