struct _ZakFormValidatorCompositePrivate
{
GNode *tree;
+ gboolean set_error;
};
G_DEFINE_TYPE_WITH_PRIVATE (ZakFormValidatorComposite, zak_form_validator_composite, ZAK_FORM_TYPE_VALIDATOR)
ZakFormElementValidator *element_validator;
ZakFormElement *element;
gchar *message;
+ gboolean valid;
} Node;
static void
ZakFormValidatorCompositePrivate *priv = zak_form_validator_composite_get_instance_private (validator);
priv->tree = NULL;
+ priv->set_error = FALSE;
}
/**
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)
{
Node *n;
n = (Node *)g_new0 (Node, 1);
+ n->valid = TRUE;
n->condition = TRUE;
Node *n;
n = (Node *)g_new0 (Node, 1);
+ n->valid = TRUE;
if (xmlStrEqual (xmlGetProp (cur, (xmlChar *)"type"), (xmlChar *)"and"))
{
Node *n;
n = (Node *)g_new0 (Node, 1);
+ n->valid = TRUE;
if (g_strcmp0 ((gchar *)xmlGetProp (cur, (xmlChar *)"validator_type"), "form") == 0)
{
}
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)
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;
{
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;
}
}
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;
}
}
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;
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;
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)
{
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
{