window.JqAjaxCore2.Validation = <% JqajaxCore2::Validations.settings.each do |name, settings| %> <% if settings[:regexp] %> validate<%= name.to_s.camelize %>: (el) -> attr = $(el).val() result = {} result.status = JqAjaxCore2.Validation.validateFormat(attr, <%= settings[:regexp] %>, <%= settings[:empty] == false ? 'false' : 'true' %>) result.message = JqAjaxCore2.ValidationErrorMessages.<%= name.to_s.camelize %> return result <% end %> # Provide getter for data attributes defined in th <% if settings[:data] %> get<%= name.to_s.camelize %>Data: (el) -> data = {} <% settings[:data].each do |m, d| %> data.<%= m.to_s.camelize %> = $(el).attr("<%= "data-#{JqajaxCore2::Validations.data_prefix}-#{m.to_s.dasherize}" %>") || "<%= d %>" <% end %> return data <% end %> <% end %> # Define Custom methods for complex validation validatePasswordSimple: (el) -> value = $(el).val() settings = JqAjaxCore2.Validation.getPasswordSimpleData(el) result = {} result.status = false result.message = JqAjaxCore2.Validation.renderErrorMessage(JqAjaxCore2.ValidationErrorMessages.PasswordSimple, settings) if JqAjaxCore2.Validation.validateRequired(el) && value.length >= parseInt(settings.Length) result.status = true result.status = false if value.match(/[A-Za-z]/) == null result.status = false if value.match(/[0-9]/) == null return result validateConfirmation: (el) -> value = $(el).val() settings = JqAjaxCore2.Validation.getConfirmationData(el) result = {} result.status = false result.message = JqAjaxCore2.Validation.renderErrorMessage(JqAjaxCore2.ValidationErrorMessages.Confirmation, settings) ref_value = $("#"+settings.ConfirmField).val() if JqAjaxCore2.Validation.validateRequired(el) && value == ref_value result.status = true return result validateExact: (el) -> value = $(el).val() settings = JqAjaxCore2.Validation.getExactData(el) result = {} result.status = false result.message = JqAjaxCore2.Validation.renderErrorMessage(JqAjaxCore2.ValidationErrorMessages.Exact, settings) if value == settings.Value result.status = true return result validateLength: (el) -> value = $(el).val() settings = JqAjaxCore2.Validation.getLengthData(el) result = {} result.status = false result.message = JqAjaxCore2.Validation.renderErrorMessage(JqAjaxCore2.ValidationErrorMessages.Length, settings) if value.length == parseInt(JqAjaxCore2.Validation.getLengthData(el).Length) result.status = true return result validateFromService: (el) -> value = $(el).val() json_url = JqAjaxCore2.Validation.getFromServiceData(el).Url result = {} values_hash = {value: value} # loading additional fields additional_fields = JSON.parse(JqAjaxCore2.Validation.getFromServiceData(el).AdditionalValues) for id, name of additional_fields values_hash[name] = $("#"+id).val() console.log(el) console.log(values_hash) console.log(json_url) request = $.ajax dataType: "json", method: 'POST', url: json_url, data: values_hash, async: false success: (data) -> result = data console.log(request) return result renderErrorMessage: (text, vars) -> t = text for attr, value of vars t = t.replace('['+attr+']', value) return t # Helper method for string validation with given regular expression validateFormat: (string, regexp, empty) -> if (!string || 0 == string.length) && empty == true return true else if string.match(regexp) == null return false else return true # run the complete validation for the given form validateForm: (form) -> form = $(form) # check if there are any fields with errors formStatus = true submitButton = $(form.find("input[type=submit]")) $(".error-description").remove() $(form.find("input")).bind 'change keydown click', -> submitButton.removeAttr("disabled") submitButton.removeClass("disabled") <% JqajaxCore2::Validations.settings.keys.each do |mode| %> inputsForValidation = form.find(":input.<%= JqajaxCore2::Validations.settings[mode][:class] %>") inputsForValidation.each -> validationResult = JqAjaxCore2.Validation.validate<%= mode.to_s.camelize %>($(this)) if validationResult.status == false && !$(this).hasClass("<%= JqajaxCore2::Validations.skip_validation_class %>") && $(this).parents(".<%= JqajaxCore2::Validations.skip_validation_class %>").length == 0 formStatus = false; JqAjaxCore2.Validation.addErrorToInput($(this), validationResult.message) else JqAjaxCore2.Validation.removeErrorFromInput($(this)) <% end %> checkboxesForValidation = form.find("input.<%= JqajaxCore2::Validations.checkbox_validation_class %>") checkboxesForValidation.each -> # Find the label for the checkbox checkbox_id = $(this).attr("id") label = $("label[for="+checkbox_id+"]") if !$(this).is(":checked") && !$(this).hasClass("<%= JqajaxCore2::Validations.skip_validation_class %>") && $(this).parents(".<%= JqajaxCore2::Validations.skip_validation_class %>").length == 0 formStatus = false label.addClass("error") else label.removeClass("error") if formStatus == false || $(form).attr("data-has-error") == "true" submitButton.attr("disabled", true) submitButton.addClass("disabled") ##alert("Bei der Eingabe sind Fehler aufgetreten. Bitte prüfen Sie die markierten Felder.") return formStatus # Bind validation to all forms that can be found initForms: -> $("input[class*=<%= JqajaxCore2::Validations.prefix %>]:first, :input[class^=<%= JqajaxCore2::Validations.prefix %>]:first").each -> $(this).closest("form").addClass("<%= JqajaxCore2::Validations.form_validation_class %>") $("form.<%= JqajaxCore2::Validations.form_validation_class %>").submit -> return JqAjaxCore2.Validation.validateForm(this) # Helper methods addErrorToInput: (el, message) -> # remove old error msg if present if $(el).prev().hasClass("<%= JqajaxCore2::Validations.error_description_class %>") $(el).prev().remove() $(el).addClass("error") # label form as error $($(el).parents("form")[0]).attr("data-has-error", "true") $(el).before("