Skip to content

Instantly share code, notes, and snippets.

@jbmyid
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbmyid/af5ffb20063ea79688b6 to your computer and use it in GitHub Desktop.
Save jbmyid/af5ffb20063ea79688b6 to your computer and use it in GitHub Desktop.
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();
})
@jbmyid
Copy link
Author

jbmyid commented Aug 7, 2014

<%= form_for @object ,data: {validate: true} do |f| %>
<%= f.text_field :email, class: "required", data:{rules: {messages: {required: "Please enter email"}}.to_json} %>
<% end %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment