type: 'get',
success: function (data) {
alert (data + element.attr('name'));
+ element.removeClass("is-invalid");
$("#spanMsg_" + element.attr('name')).remove();
- if (data != '')
+ if (data != '\n')
{
element.addClass("is-invalid");
element.parent().append("<span id=\"spanMsg_" + element.attr('name') + "\" class=\"invalid-feedback\">" + data + "</span>");
}
});
});
+
+ $('#myForm').on('submit', function () {
+ alert("submit " + $(this).attr('name'));
+
+ $("#birthday").remove();
+ });
});
function click_add ()
$("#name").removeClass("is-invalid");
$("#spanName").remove();
}
+
+function click_validate_form ()
+{
+ alert ("validate form");
+
+ $.ajax ({
+ url: '/zakformtests/cgi_validate',
+ //url: '/cgi-bin/env',
+ method: 'post',
+ data: $("#name").serialize(),
+ dataType: 'json',
+ cache: false,
+ success: function (data) {
+ alert ('success ' + data);
+ },
+ error: function (xhr, status, error) {
+ alert (xhr.status + ' ' + xhr.statusText);
+ }
+ });
+}
#include <libzakform/libzakform.h>
#include <libzakformcgi/libzakformcgi.h>
#include <libzakutils/libzakutils.h>
+#include <libzakutilsjsonxml/libzakutilsjsonxml.h>
int
main (int argc, char *argv[])
{
ZakCgiMain *zakcgimain;
+ GString *header;
+
GString *str;
GValue *val;
ZakFormElement *element;
+ header = NULL;
+
setlocale (LC_ALL, "");
zakcgimain = zak_cgi_main_new ();
+ zak_cgi_main_get_stdin (zakcgimain);
str = g_string_new ("");
else
{
g_warning ("RET no field");
- zak_cgi_main_out (NULL, str->str);
- return 0;
}
val = zak_cgi_main_get_parameter (zakcgimain, "v");
else
{
g_warning ("RET no value");
+ }
+
+ if (zak_cgi_main_is_get (zakcgimain)
+ && (field == NULL
+ || value == NULL)
+ )
+ {
zak_cgi_main_out (NULL, str->str);
return 0;
}
if (zak_form_form_load_from_file (ZAK_FORM_FORM (form), XMLDIR "/cgi.xml"))
{
- element = zak_form_form_get_element_by_id (ZAK_FORM_FORM (form), field);
- if (element != NULL)
+ if (zak_cgi_main_is_get (zakcgimain))
{
- zak_form_cgi_form_element_bind (ZAK_FORM_CGI_FORM_ELEMENT (element), zak_utils_gvalue_new_string (value));
- if (!zak_form_element_is_valid (element))
+ /* validate single element of the form */
+
+ element = zak_form_form_get_element_by_id (ZAK_FORM_FORM (form), field);
+ if (element != NULL)
{
- g_warning ("RET not valid");
+ zak_form_cgi_form_element_bind (ZAK_FORM_CGI_FORM_ELEMENT (element), zak_utils_gvalue_new_string (value));
+ if (!zak_form_element_is_valid (element))
+ {
+ g_warning ("RET not valid");
- guint i;
- GPtrArray *messages = zak_form_element_get_messages (element);
+ guint i;
+ GPtrArray *messages = zak_form_element_get_messages (element);
- for (i = 0; i < messages->len; i++)
- {
- g_string_append_printf (str, "%s<br/>\n", (gchar *)g_ptr_array_index (messages, i));
+ for (i = 0; i < messages->len; i++)
+ {
+ g_string_append_printf (str, "%s<br/>\n", (gchar *)g_ptr_array_index (messages, i));
+ }
}
}
+ else
+ {
+ g_string_append_printf (str, "Element «%s» not found", field);
+ }
}
else
{
- g_string_append_printf (str, "Element «%s» not found", field);
+ /* validate the form */
+ GPtrArray *ar_elements;
+ GPtrArray *messages;
+ guint i;
+
+ JsonBuilder *builder;
+ JsonGenerator *generator;
+
+ zak_form_cgi_form_bind (form);
+
+ builder = json_builder_new ();
+ json_builder_begin_array (builder);
+
+ ar_elements = zak_form_form_get_elements (ZAK_FORM_FORM (form));
+ for (i = 0; i < ar_elements->len; i++)
+ {
+ ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (ar_elements, i);
+
+ json_builder_begin_object (builder);
+ zak_utils_json_set_string (builder,
+ zak_form_element_get_name (element),
+ zak_form_element_get_value (element));
+ json_builder_end_object (builder);
+ /* /\* if (!zak_form_element_is_valid (element)) *\/ */
+ /* /\* { *\/ */
+ /* /\* messages = zak_form_element_get_messages (element); *\/ */
+
+ /* /\* for (i = 0; i < messages->len; i++) *\/ */
+ /* /\* { *\/ */
+ /* /\* g_string_append_printf (str, "%s<br/>\n", (gchar *)g_ptr_array_index (messages, i)); *\/ */
+ /* /\* } *\/ */
+ /* /\* } *\/ */
+ }
+
+ json_builder_end_array (builder);
+
+ generator = json_generator_new ();
+ json_generator_set_root (generator, json_builder_get_root (builder));
+
+ g_string_assign (str, json_generator_to_data (generator, NULL));
+
+ g_object_unref (builder);
+ g_object_unref (generator);
+
+ /* g_ptr_array_set_size (priv->ar_messages, 0); */
+ /* for (i = 0; i < priv->ar_validators->len; i++) */
+ /* { */
+ /* ZakFormValidator *validator = (ZakFormValidator *)g_ptr_array_index (priv->ar_validators, i); */
+ /* if (!zak_form_validator_validate (validator)) */
+ /* { */
+ /* g_ptr_array_add (priv->ar_messages, (gpointer)g_strdup (zak_form_validator_get_message (validator))); */
+ /* ret = FALSE; */
+ /* } */
+ /* } */
+
+ header = g_string_new ("Content-Type: application/json");
}
}
g_warning("RET %s",str->str);
- zak_cgi_main_out (NULL, str->str);
+ zak_cgi_main_out (header->str, str->str);
+
+ g_string_free (header, TRUE);
+ g_string_free (str, TRUE);
return 0;
}