]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Multilevel for ZakFormValidatorComposite.
authorAndrea Zagli <azagli@libero.it>
Sun, 14 May 2017 15:26:17 +0000 (17:26 +0200)
committerAndrea Zagli <azagli@libero.it>
Thu, 18 May 2017 13:32:52 +0000 (15:32 +0200)
src/formvalidatorcomposite.c

index 1a11f297b42e920fbe780c17845e97824e10402d..c652b0349a57c4d19b27b3dc8315ed7b16528915 100644 (file)
@@ -154,6 +154,7 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                                                {
                                                        g_warning ("Logic type «%s» not supported.",
                                                                   xmlGetProp (cur, (xmlChar *)"type"));
+                                                       cur = cur->next;
                                                        continue;
                                                }
 
@@ -182,6 +183,7 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                                                {
                                                        g_warning ("Element «%s» not present in form.",
                                                                   (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
+                                                       cur = cur->next;
                                                        continue;
                                                }
 
@@ -192,6 +194,7 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                                                {
                                                        g_warning ("Element «%s» not found.",
                                                                   (gchar *)xmlGetProp (cur, (xmlChar *)"type"));
+                                                       cur = cur->next;
                                                        continue;
                                                }
 
@@ -214,26 +217,6 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                }
 }
 
-static gboolean
-traverse_func (GNode *node,
-               gpointer data)
-{
-       ZakFormValidatorCompositePrivate *priv = ZAK_FORM_VALIDATOR_COMPOSITE_GET_PRIVATE ((ZakFormValidator *)data);
-
-       Node *n = (Node *)node->data;
-
-       if (n->type > 0)
-               {
-                       g_warning ("Node logic type %d message «%s»", n->type, n->message);
-               }
-       else
-               {
-                       g_warning ("Node validator for element «%s»", zak_form_element_get_name (n->element));
-               }
-
-       return FALSE;
-}
-
 /**
  * zak_form_validator_composite_xml_parsing:
  * @validator:
@@ -249,11 +232,6 @@ zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode *
 
        _zak_form_validator_composite_xml_parsing (validator, xnode, (ZakFormForm *)form, NULL);
 
-       if (priv->tree != NULL)
-               {
-                       g_node_traverse (priv->tree, G_PRE_ORDER, G_TRAVERSE_ALL, -1, traverse_func, (gpointer)validator);
-               }
-
        return TRUE;
 }
 
@@ -317,11 +295,10 @@ zak_form_validator_composite_finalize (GObject *gobject)
 }
 
 static gboolean
-zak_form_validator_composite_validate (ZakFormValidator *validator)
+_zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *parent)
 {
        gboolean ret;
 
-       GNode *root;
        GNode *child;
        Node *n;
        Node *n_child;
@@ -329,54 +306,76 @@ zak_form_validator_composite_validate (ZakFormValidator *validator)
        guint i;
        guint children;
 
-       ZakFormValidatorCompositePrivate *priv = ZAK_FORM_VALIDATOR_COMPOSITE_GET_PRIVATE (validator);
+       n = (Node *)parent->data;
 
-       if (priv->tree != NULL)
+       switch (n->type)
                {
-                       root = g_node_get_root (priv->tree);
+               case LOGIC_TYPE_IF:
+                       break;
 
-                       n = (Node *)root->data;
+               case LOGIC_TYPE_AND:
+                       ret = TRUE;
+                       break;
+
+               case LOGIC_TYPE_OR:
+                       ret = FALSE;
+                       break;
+               }
 
+       children = g_node_n_children (parent);
+       for (i = 0; i < children; i++)
+               {
+                       child = g_node_nth_child (parent, i);
+                       n_child = (Node *)child->data;
                        switch (n->type)
                                {
                                case LOGIC_TYPE_IF:
                                        break;
 
                                case LOGIC_TYPE_AND:
-                                       ret = TRUE;
-                                       break;
-
-                               case LOGIC_TYPE_OR:
-                                       ret = FALSE;
-                                       break;
-                               }
-
-                       children = g_node_n_children (root);
-                       for (i = 0; i < children; i++)
-                               {
-                                       child = g_node_nth_child (root, i);
-                                       n_child = (Node *)child->data;
-                                       switch (n->type)
+                                       if (n_child->type > 0)
+                                               {
+                                                       ret = (ret && _zak_form_validator_composite_validate (validator, child));
+                                               }
+                                       else
                                                {
-                                               case LOGIC_TYPE_IF:
-                                                       break;
-
-                                               case LOGIC_TYPE_AND:
                                                        ret = (ret && zak_form_element_validator_validate (n_child->validator,
                                                                                                           zak_form_element_get_value (n_child->element)));
-                                                       break;
+                                               }
+                                       break;
 
-                                               case LOGIC_TYPE_OR:
+                               case LOGIC_TYPE_OR:
+                                       if (n_child->type > 0)
+                                               {
+                                                       ret = (ret || _zak_form_validator_composite_validate (validator, child));
+                                               }
+                                       else
+                                               {
                                                        ret = (ret || zak_form_element_validator_validate (n_child->validator,
                                                                                                           zak_form_element_get_value (n_child->element)));
-                                                       break;
                                                }
+                                       break;
                                }
+               }
 
-                       if (!ret)
-                               {
-                                       zak_form_validator_set_message (ZAK_FORM_VALIDATOR (validator), n->message);
-                               }
+       if (!ret)
+               {
+                       zak_form_validator_set_message (ZAK_FORM_VALIDATOR (validator), n->message);
+               }
+
+       return ret;
+}
+
+static gboolean
+zak_form_validator_composite_validate (ZakFormValidator *validator)
+{
+       gboolean ret;
+
+       ZakFormValidatorCompositePrivate *priv = ZAK_FORM_VALIDATOR_COMPOSITE_GET_PRIVATE (validator);
+
+       if (priv->tree != NULL)
+               {
+                       ret = _zak_form_validator_composite_validate (validator, g_node_get_root (priv->tree));
                }
        else
                {