Last active
August 29, 2015 14:05
-
-
Save jbmyid/af5ffb20063ea79688b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FormValidator = function(form){ | |
this.forms = form || $("form[data-validate=true]"); | |
this.setDefaults = function(){ | |
$.validator.setDefaults({ | |
errorElement: "span" | |
}) | |
$.extend(jQuery.validator.messages, { | |
required: "Mandatory Field" | |
}) | |
} | |
this.validateForms = function(f){ | |
f.validate(); | |
} | |
// "data-rules"=>{messages: {required: 'Required input'}}.to_json | |
this.setRules = function(newRules){ | |
$("input", this.forms).each(function(i,v){ | |
ele = $(v); | |
rules = newRules || ele.data("rules") | |
if(rules){ | |
ele.rules("add", rules) | |
} | |
}) | |
} | |
this.setAll = function(){ | |
this.setDefaults() | |
this.validateForms(this.forms) | |
this.setRules() | |
} | |
this.setAll() | |
} | |
$(function(){ | |
var validator = new FormValidator(); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<%= form_for @object ,data: {validate: true} do |f| %>
<%= f.text_field :email, class: "required", data:{rules: {messages: {required: "Please enter email"}}.to_json} %>
<% end %>