]> saetta.ns0.it Git - zakform/libzakform/commitdiff
Enable ZakFormValidator inside ZakFormValidatorComposite.
authorAndrea Zagli <azagli@libero.it>
Mon, 13 Apr 2020 08:45:27 +0000 (10:45 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 13 Apr 2020 08:45:27 +0000 (10:45 +0200)
src/formvalidatorcomposite.c

index a6911aa3a39476963135ec525efdc94566ec9a05..6b95e6aed8bb62e6403b3f5e0a644cf0325e7056 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
@@ -77,7 +77,8 @@ typedef struct
                gboolean condition;
                gboolean condition_type;
                LogicType logic_type;
-               ZakFormElementValidator *validator;
+               ZakFormValidator *form_validator;
+               ZakFormElementValidator *element_validator;
                ZakFormElement *element;
                gchar *message;
        } Node;
@@ -135,7 +136,6 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                        if (xmlStrEqual (cur->name, (xmlChar *)"condition"))
                                {
                                        ZakFormElement *element;
-                                       ZakFormElementValidatorConstructorFunc validator_constructor;
                                        GNode *gnode;
                                        Node *n;
 
@@ -145,28 +145,53 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
 
                                        n->condition_type= zak_utils_string_to_boolean ((gchar *)xmlGetProp (cur, (xmlChar *)"condition_type"));
 
-                                       element = zak_form_form_get_element_by_id (form, (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
-                                       if (!ZAK_FORM_IS_ELEMENT (element))
+                                       if (g_strcmp0((gchar *)xmlGetProp (cur, (xmlChar *)"validator_type"), "form") == 0)
                                                {
-                                                       g_warning ("Element «%s» not present in form.",
-                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
-                                                       cur = cur->next;
-                                                       continue;
-                                               }
+                                                       ZakFormValidatorConstructorFunc validator_constructor;
 
-                                       validator_constructor = zak_form_get_form_element_validator ((gchar *)xmlGetProp (cur, (xmlChar *)"validator"));
-                                       if (validator_constructor == NULL)
-                                               {
-                                                       g_warning ("Validator «%s» not found.",
-                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"validator"));
-                                                       cur = cur->next;
-                                                       continue;
+                                                       n->element_validator = NULL;
+
+                                                       validator_constructor = zak_form_get_form_validator ((gchar *)xmlGetProp (cur, (xmlChar *)"validator"));
+                                                       if (validator_constructor == NULL)
+                                                               {
+                                                                       g_warning ("Validator «%s» not found.",
+                                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"validator"));
+                                                                       cur = cur->next;
+                                                                       continue;
+                                                               }
+
+                                                       n->form_validator = validator_constructor ();
+                                                       zak_form_validator_xml_parsing (n->form_validator, cur, form);
                                                }
+                                       else
+                                               {
+                                                       ZakFormElementValidatorConstructorFunc validator_constructor;
 
-                                       n->validator = validator_constructor ();
-                                       zak_form_element_validator_xml_parsing (n->validator, cur);
+                                                       n->form_validator = NULL;
 
-                                       n->element = element;
+                                                       element = zak_form_form_get_element_by_id (form, (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
+                                                       if (!ZAK_FORM_IS_ELEMENT (element))
+                                                               {
+                                                                       g_warning ("Element «%s» not present in form.",
+                                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
+                                                                       cur = cur->next;
+                                                                       continue;
+                                                               }
+
+                                                       validator_constructor = zak_form_get_form_element_validator ((gchar *)xmlGetProp (cur, (xmlChar *)"validator"));
+                                                       if (validator_constructor == NULL)
+                                                               {
+                                                                       g_warning ("Validator «%s» not found.",
+                                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"validator"));
+                                                                       cur = cur->next;
+                                                                       continue;
+                                                               }
+
+                                                       n->element_validator = validator_constructor ();
+                                                       zak_form_element_validator_xml_parsing (n->element_validator, cur);
+
+                                                       n->element = element;
+                                               }
 
                                        n->message = NULL;
 
@@ -224,33 +249,59 @@ _zak_form_validator_composite_xml_parsing (ZakFormValidator *validator, xmlNode
                        else if (xmlStrEqual (cur->name, (xmlChar *)"validator"))
                                {
                                        ZakFormElement *element;
-                                       ZakFormElementValidatorConstructorFunc validator_constructor;
                                        Node *n;
 
-                                       element = zak_form_form_get_element_by_id (form, (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
-                                       if (!ZAK_FORM_IS_ELEMENT (element))
+                                       if (g_strcmp0 ((gchar *)xmlGetProp (cur, (xmlChar *)"element"), "form") == 0)
                                                {
-                                                       g_warning ("Element «%s» not present in form.",
-                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
-                                                       cur = cur->next;
-                                                       continue;
-                                               }
+                                                       ZakFormValidatorConstructorFunc validator_constructor;
 
-                                       n = (Node *)g_new0 (Node, 1);
+                                                       n->element_validator = NULL;
 
-                                       validator_constructor = zak_form_get_form_element_validator ((gchar *)xmlGetProp (cur, (xmlChar *)"type"));
-                                       if (validator_constructor == NULL)
-                                               {
-                                                       g_warning ("Validator «%s» not found.",
-                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"type"));
-                                                       cur = cur->next;
-                                                       continue;
+                                                       n = (Node *)g_new0 (Node, 1);
+
+                                                       validator_constructor = zak_form_get_form_validator ((gchar *)xmlGetProp (cur, (xmlChar *)"type"));
+                                                       if (validator_constructor == NULL)
+                                                               {
+                                                                       g_warning ("Validator «%s» not found.",
+                                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"type"));
+                                                                       cur = cur->next;
+                                                                       continue;
+                                                               }
+
+                                                       n->form_validator = validator_constructor ();
+                                                       zak_form_validator_xml_parsing (n->element_validator, cur, form);
                                                }
+                                       else
+                                               {
+                                                       ZakFormElementValidatorConstructorFunc validator_constructor;
+
+                                                       n->form_validator = NULL;
 
-                                       n->validator = validator_constructor ();
-                                       zak_form_element_validator_xml_parsing (n->validator, cur);
+                                                       element = zak_form_form_get_element_by_id (form, (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
+                                                       if (!ZAK_FORM_IS_ELEMENT (element))
+                                                               {
+                                                                       g_warning ("Element «%s» not present in form.",
+                                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"element"));
+                                                                       cur = cur->next;
+                                                                       continue;
+                                                               }
 
-                                       n->element = element;
+                                                       n = (Node *)g_new0 (Node, 1);
+
+                                                       validator_constructor = zak_form_get_form_element_validator ((gchar *)xmlGetProp (cur, (xmlChar *)"type"));
+                                                       if (validator_constructor == NULL)
+                                                               {
+                                                                       g_warning ("Validator «%s» not found.",
+                                                                                  (gchar *)xmlGetProp (cur, (xmlChar *)"type"));
+                                                                       cur = cur->next;
+                                                                       continue;
+                                                               }
+
+                                                       n->element_validator = validator_constructor ();
+                                                       zak_form_element_validator_xml_parsing (n->element_validator, cur);
+
+                                                       n->element = element;
+                                               }
 
                                        if (tree == NULL)
                                                {
@@ -376,18 +427,37 @@ _zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *pare
 
                        if (n_child->condition)
                                {
-                                       if (zak_form_element_validator_validate (n_child->validator,
-                                                                                zak_form_element_get_value (n_child->element)) == n_child->condition_type)
+                                       if (n_child->form_validator != NULL)
                                                {
-                                                       switch (n->logic_type)
+                                                       if (zak_form_validator_validate (n_child->element_validator) == n_child->condition_type)
                                                                {
-                                                               case LOGIC_TYPE_AND:
-                                                                       ret = (ret && _zak_form_validator_composite_validate (validator, child));
-                                                                       break;
-
-                                                               case LOGIC_TYPE_OR:
-                                                                       ret = (ret || _zak_form_validator_composite_validate (validator, child));
-                                                                       break;
+                                                                       switch (n->logic_type)
+                                                                               {
+                                                                               case LOGIC_TYPE_AND:
+                                                                                       ret = (ret && _zak_form_validator_composite_validate (validator, child));
+                                                                                       break;
+
+                                                                               case LOGIC_TYPE_OR:
+                                                                                       ret = (ret || _zak_form_validator_composite_validate (validator, child));
+                                                                                       break;
+                                                                               }
+                                                               }
+                                               }
+                                       else
+                                               {
+                                                       if (zak_form_element_validator_validate (n_child->element_validator,
+                                                                                                zak_form_element_get_value (n_child->element)) == n_child->condition_type)
+                                                               {
+                                                                       switch (n->logic_type)
+                                                                               {
+                                                                               case LOGIC_TYPE_AND:
+                                                                                       ret = (ret && _zak_form_validator_composite_validate (validator, child));
+                                                                                       break;
+
+                                                                               case LOGIC_TYPE_OR:
+                                                                                       ret = (ret || _zak_form_validator_composite_validate (validator, child));
+                                                                                       break;
+                                                                               }
                                                                }
                                                }
                                }
@@ -402,8 +472,15 @@ _zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *pare
                                                                }
                                                        else
                                                                {
-                                                                       ret = (ret && zak_form_element_validator_validate (n_child->validator,
-                                                                                                                          zak_form_element_get_value (n_child->element)));
+                                                                       if (n_child->form_validator != NULL)
+                                                                               {
+                                                                                       ret = (ret && zak_form_validator_validate (n_child->form_validator));
+                                                                               }
+                                                                       else
+                                                                               {
+                                                                                       ret = (ret && zak_form_element_validator_validate (n_child->element_validator,
+                                                                                                                                          zak_form_element_get_value (n_child->element)));
+                                                                               }
                                                                }
                                                        break;
 
@@ -414,8 +491,15 @@ _zak_form_validator_composite_validate (ZakFormValidator *validator, GNode *pare
                                                                }
                                                        else
                                                                {
-                                                                       ret = (ret || zak_form_element_validator_validate (n_child->validator,
-                                                                                                                          zak_form_element_get_value (n_child->element)));
+                                                                       if (n_child->form_validator != NULL)
+                                                                               {
+                                                                                       ret = (ret || zak_form_validator_validate (n_child->form_validator));
+                                                                               }
+                                                                       else
+                                                                               {
+                                                                                       ret = (ret || zak_form_element_validator_validate (n_child->element_validator,
+                                                                                                                                          zak_form_element_get_value (n_child->element)));
+                                                                               }
                                                                }
                                                        break;
                                                }