From: Andrea Zagli Date: Mon, 1 May 2017 10:44:09 +0000 (+0200) Subject: ZakFormElementValidatorDate can now compare the value. X-Git-Tag: v0.1.0~1^2~17 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=3337883ae86e8cd775bab9a61270fa975d1e66df;p=zakform%2Flibzakform ZakFormElementValidatorDate can now compare the value. --- diff --git a/po/it.po b/po/it.po index aa1eb2a..f249455 100644 --- a/po/it.po +++ b/po/it.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: libzakform 0.0.1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2017-05-01 09:58+0200\n" +"POT-Creation-Date: 2017-05-01 11:19+0200\n" "PO-Revision-Date: 2016-01-02 12:44+0200\n" "Last-Translator: Andrea Zagli \n" "Language-Team: Italian \n" @@ -17,31 +17,31 @@ msgstr "" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n != 1);\n" -#: ../src/commons.c:89 +#: ../src/commons.c:90 msgid "lesser than" msgstr "minore di" -#: ../src/commons.c:93 +#: ../src/commons.c:94 msgid "lesser or equal to" msgstr "minore o uguale a" -#: ../src/commons.c:97 +#: ../src/commons.c:98 msgid "equal to" msgstr "uguale a" -#: ../src/commons.c:101 +#: ../src/commons.c:102 msgid "different from" msgstr "diverso da" -#: ../src/commons.c:105 +#: ../src/commons.c:106 msgid "greater than" msgstr "maggiore di" -#: ../src/commons.c:109 +#: ../src/commons.c:110 msgid "greater or equal to" msgstr "maggiore o uguale a" -#: ../src/commons.c:113 +#: ../src/commons.c:114 msgid "NOT SUPPORTED" msgstr "NON SUPPORTATO" @@ -80,6 +80,11 @@ msgstr "I moduli non sono supportati in questo sistema operativo" msgid "Invalid value" msgstr "Valore non valido" +#: ../src/formelementvalidatordate.c:364 +#, c-format +msgid "Value must be %s %s" +msgstr "Il valore deve essere %s %s" + #: ../src/formvalidatorcompare.c:253 ../src/formvalidatorcomparedate.c:266 #, c-format msgid "«%s» must be %s «%s»" diff --git a/src/commons.c b/src/commons.c index f1f604e..a1d2892 100644 --- a/src/commons.c +++ b/src/commons.c @@ -67,6 +67,7 @@ zak_form_get_compare_type_from_string (const gchar *str) else { g_warning ("Compare type «%s» not supported.", str); + ret = ZAK_FORM_COMPARE_TYPE_NONE; } return ret; diff --git a/src/commons.h b/src/commons.h index 18e0b66..87f7452 100644 --- a/src/commons.h +++ b/src/commons.h @@ -29,6 +29,7 @@ G_BEGIN_DECLS typedef enum { + ZAK_FORM_COMPARE_TYPE_NONE, ZAK_FORM_COMPARE_TYPE_LESSER, ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL, ZAK_FORM_COMPARE_TYPE_EQUAL, diff --git a/src/formelementvalidatordate.c b/src/formelementvalidatordate.c index 072ab4e..b4130cb 100644 --- a/src/formelementvalidatordate.c +++ b/src/formelementvalidatordate.c @@ -20,6 +20,8 @@ #include #endif +#include + #include #include "formelementvalidator.h" @@ -55,6 +57,8 @@ struct _ZakFormElementValidatorDate typedef struct _ZakFormElementValidatorDatePrivate ZakFormElementValidatorDatePrivate; struct _ZakFormElementValidatorDatePrivate { + ZakFormCompareType compare_type; + gchar *compare_value; gchar *format; }; @@ -100,6 +104,80 @@ ZakFormElementValidatorDate return zak_form_element_validator_date; } +/** + * zak_form_element_validator_date_set_compare_type: + * @validator: + * @type: + * + */ +void +zak_form_element_validator_date_set_compare_type (ZakFormElementValidatorDate *validator, ZakFormCompareType type) +{ + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator); + + priv->compare_type = type; +} + +/** + * zak_form_element_validator_date_get_compare_type: + * @validator: + * + */ +ZakFormCompareType +zak_form_element_validator_date_get_compare_type (ZakFormElementValidatorDate *validator) +{ + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator); + + return priv->compare_type; +} + +/** + * zak_form_element_validator_date_set_compare_value: + * @validator: + * @value: + * + */ +void +zak_form_element_validator_date_set_compare_value (ZakFormElementValidatorDate *validator, const gchar *value) +{ + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator); + + if (priv->compare_value != NULL) + { + g_free (priv->compare_value); + priv->compare_value = NULL; + } + + if (value != NULL) + { + priv->compare_value = g_strdup (value); + } +} + +/** + * zak_form_element_validator_date_get_compare_value: + * @validator: + * + */ +gchar +*zak_form_element_validator_date_get_compare_value (ZakFormElementValidatorDate *validator) +{ + gchar *ret; + + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator); + + if (priv->compare_value == NULL) + { + ret = NULL; + } + else + { + ret = g_strdup (priv->compare_value); + } + + return ret; +} + /** * zak_form_element_validator_date_set_format: * @validator: @@ -140,8 +218,18 @@ gchar static gboolean zak_form_element_validator_date_xml_parsing (ZakFormElementValidator *validator, xmlNode *xnode) { + gchar *prop; + ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator)); + prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"type_comp"); + zak_form_element_validator_date_set_compare_type (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator), zak_form_get_compare_type_from_string (prop)); + g_free (prop); + + prop = (gchar *)xmlGetProp (xnode, (const xmlChar *)"value_comp"); + zak_form_element_validator_date_set_compare_value (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator), prop); + g_free (prop); + zak_form_element_validator_date_set_format (ZAK_FORM_ELEMENT_VALIDATOR_DATE (validator), (gchar *)xmlNodeGetContent (xnode)); return TRUE; @@ -213,11 +301,18 @@ zak_form_element_validator_date_validate (ZakFormElementValidator *validator_dat gboolean ret; gchar *_value; GDateTime *gdt; + GDateTime *gdt_compare; + + gint comp; + gchar *msg; ZakFormElementValidatorDatePrivate *priv = ZAK_FORM_ELEMENT_VALIDATOR_DATE_GET_PRIVATE (validator_date); g_return_val_if_fail (value != NULL, FALSE); + gdt = NULL; + gdt_compare = NULL; + _value = g_strstrip (g_strdup (value)); if (g_strcmp0 (_value, "") == 0) { @@ -228,6 +323,62 @@ zak_form_element_validator_date_validate (ZakFormElementValidator *validator_dat gdt = zak_utils_get_gdatetime_from_string (_value, priv->format); ret = (gdt != NULL); + if (ret + && priv->compare_type > ZAK_FORM_COMPARE_TYPE_NONE) + { + if (g_strcmp0 (priv->compare_value, "@now") == 0) + { + GDateTime *gdt_tmp; + gchar *gdt_str; + + gdt_tmp = g_date_time_new_now_local (); + gdt_str = zak_utils_gdatetime_format (gdt_tmp, priv->format); + gdt_compare = zak_utils_get_gdatetime_from_string (gdt_str, priv->format); + + g_free (gdt_str); + g_date_time_unref (gdt_tmp); + } + else + { + gdt_compare = zak_utils_get_gdatetime_from_string (priv->compare_value, priv->format); + } + + if (gdt_compare != NULL) + { + comp = g_date_time_compare (gdt, gdt_compare); + switch (comp) + { + case -1: + ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_LESSER + || priv->compare_type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL); + break; + + case 0: + ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL + || priv->compare_type == ZAK_FORM_COMPARE_TYPE_EQUAL + || priv->compare_type == ZAK_FORM_COMPARE_TYPE_GREATER_EQUAL); + break; + + case 1: + ret = (priv->compare_type == ZAK_FORM_COMPARE_TYPE_GREATER + || priv->compare_type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL); + break; + }; + + if (!ret) + { + msg = g_strdup_printf (_("Value must be %s %s"), + zak_form_get_compare_type_stringify (priv->compare_type), + zak_utils_gdatetime_format (gdt_compare, priv->format)); + zak_form_element_validator_set_message (validator_date, msg); + g_free (msg); + } + + g_date_time_unref (gdt_compare); + } + + } + if (gdt != NULL) { g_date_time_unref (gdt); diff --git a/src/formelementvalidatordate.h b/src/formelementvalidatordate.h index 84cf21c..d5d489d 100644 --- a/src/formelementvalidatordate.h +++ b/src/formelementvalidatordate.h @@ -24,6 +24,7 @@ #include +#include "commons.h" #include "formelementvalidator.h" @@ -35,6 +36,12 @@ G_DECLARE_FINAL_TYPE (ZakFormElementValidatorDate, zak_form_element_validator_da ZakFormElementValidatorDate *zak_form_element_validator_date_new (void); +void zak_form_element_validator_date_set_compare_type (ZakFormElementValidatorDate *validator, ZakFormCompareType type); +ZakFormCompareType zak_form_element_validator_date_get_compare_type (ZakFormElementValidatorDate *validator); + +void zak_form_element_validator_date_set_compare_value (ZakFormElementValidatorDate *validator, const gchar *value); +gchar *zak_form_element_validator_date_get_compare_value (ZakFormElementValidatorDate *validator); + void zak_form_element_validator_date_set_format (ZakFormElementValidatorDate *validator, const gchar *format); gchar *zak_form_element_validator_date_get_format (ZakFormElementValidatorDate *validator); diff --git a/src/formvalidatorcompare.c b/src/formvalidatorcompare.c index c18b2de..68a4e5e 100644 --- a/src/formvalidatorcompare.c +++ b/src/formvalidatorcompare.c @@ -232,7 +232,6 @@ zak_form_validator_compare_validate (ZakFormValidator *validator) if (comp < 0) { ret = (priv->type == ZAK_FORM_COMPARE_TYPE_LESSER - || priv->type == ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL || priv->type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL); } else if (comp == 0) @@ -244,7 +243,6 @@ zak_form_validator_compare_validate (ZakFormValidator *validator) else if (comp > 0) { ret = (priv->type == ZAK_FORM_COMPARE_TYPE_GREATER - || priv->type == ZAK_FORM_COMPARE_TYPE_GREATER_EQUAL || priv->type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL); }; diff --git a/src/formvalidatorcomparedate.c b/src/formvalidatorcomparedate.c index 2205a80..ab7dc82 100644 --- a/src/formvalidatorcomparedate.c +++ b/src/formvalidatorcomparedate.c @@ -243,7 +243,6 @@ zak_form_validator_compare_date_validate (ZakFormValidator *validator) { case -1: ret = (priv->type == ZAK_FORM_COMPARE_TYPE_LESSER - || priv->type == ZAK_FORM_COMPARE_TYPE_LESSER_EQUAL || priv->type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL); break; @@ -255,7 +254,6 @@ zak_form_validator_compare_date_validate (ZakFormValidator *validator) case 1: ret = (priv->type == ZAK_FORM_COMPARE_TYPE_GREATER - || priv->type == ZAK_FORM_COMPARE_TYPE_GREATER_EQUAL || priv->type == ZAK_FORM_COMPARE_TYPE_NOT_EQUAL); break; };