$(document).ready (function (){
- $("#birthday").focusout (function (){
- alert("focusout");
+ // $("#birthday").focusout (function (){
+ // alert("focusout");
- var element = $(this);
- $.ajax ({
- url: '/zakformtests/cgi_validate',
- data: 'f=' + $(this).attr('name') + '&v=' + $(this).val(),
- type: 'get',
- success: function (data) {
- alert (data + element.attr('name'));
- element.removeClass("is-invalid");
- $("#spanMsg_" + element.attr('name')).remove();
- if (data != '\n')
- {
- element.addClass("is-invalid");
- element.parent().append("<span id=\"spanMsg_" + element.attr('name') + "\" class=\"invalid-feedback\">" + data + "</span>");
- }
- }
- });
- });
+ // var element = $(this);
+ // $.ajax ({
+ // url: '/zakformtests/cgi_validate',
+ // data: 'f=' + $(this).attr('name') + '&v=' + $(this).val(),
+ // type: 'get',
+ // success: function (data) {
+ // alert (data + element.attr('name'));
+ // element.removeClass("is-invalid");
+ // $("#spanMsg_" + element.attr('name')).remove();
+ // 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'));
});
});
+function set_error (value, idx, array)
+{
+ if (value.element == '') return;
+
+ if (value.messages.length == 0)
+ {
+ clear_error (value.element);
+ }
+ else
+ {
+ if ($('#messages').attr('hidden') !== typeof undefined)
+ {
+ $('#messages').removeAttr('hidden');
+ $('#messages').html('Form not valid!!!');
+ }
+
+ $('#' + value.element).addClass("is-invalid");
+ $('#' + value.element).parent().append("<span id=\"spanMsg_" + value.element + "\" class=\"invalid-feedback\">" + value.messages[0] + "</span>");
+ }
+}
+
+function clear_error (field)
+{
+ $('#' + field).removeClass("is-invalid");
+ $("#spanMsg_" + field).remove();
+}
+
function click_add ()
{
alert ("add");
function click_validate_form ()
{
- alert ("validate form");
+ //alert ("validate form");
$.ajax ({
url: '/zakformtests/cgi_validate',
//url: '/cgi-bin/env',
method: 'post',
- data: $("#name").serialize(),
+ data: $('#myForm').serialize(),
+ //contentType: 'multipart/form-data',
dataType: 'json',
cache: false,
success: function (data) {
- alert ('success ' + data);
+ //alert ('success ' + data);
+ $('#messages').attr('hidden', '');
+ $('#messages').html('');
+
+ data.forEach(set_error);
},
error: function (xhr, status, error) {
alert (xhr.status + ' ' + xhr.statusText);
GPtrArray *ar_elements;
GPtrArray *messages;
guint i;
+ guint m;
JsonBuilder *builder;
JsonGenerator *generator;
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));
+
+ zak_utils_json_set_string (builder, "element", zak_form_element_get_name (element));
+
+ json_builder_begin_object (builder);
+
+ json_builder_set_member_name (builder, "messages");
+
+ json_builder_begin_array (builder);
+
+ if (!zak_form_element_is_valid (element))
+ {
+
+ messages = zak_form_element_get_messages (element);
+
+ for (m = 0; m < messages->len; m++)
+ {
+ json_builder_add_string_value (builder, (gchar *)g_ptr_array_index (messages, m));
+ }
+
+ }
+ json_builder_end_array (builder);
+
+ json_builder_end_object (builder);
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);