Created
April 18, 2016 07:52
-
-
Save lareb/16ef3093d3d4e21d58f3f8c9d8a4ac17 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
class MyController | |
include One | |
def create | |
params = {first_name: "lareb", last_name: "nawab", email: "[email protected]", | |
mobile: "8087036184", address_line_1: "2171 Parmar nagar 5", address_line_2: "Fatima Nagar", | |
city: "Pune", state: "Maharashtra", country: "India", zip: "411013"} | |
#this will return an array of validation message | |
validate(params) | |
end | |
end | |
module One | |
def validate(params) | |
cannot_empty = [:first_name, :last_name, :email] | |
email_validation = [:email] | |
zip_validation = [:zip] | |
error_messages = [] | |
cannot_empty.each do |attr| | |
#attr has :first_name, :last_name, :email values | |
error_messages << cannot_be_empty(params[attr]) | |
end | |
email_validation.each do |attr| | |
#attr has :email value | |
error_messages << get_email_validation(params[attr]) | |
end | |
zip_validation.each do |attr| | |
#attr has :email value | |
error_messages << get_zip_validation(params[attr]) | |
end | |
end | |
def cannot_be_empty(attr) | |
return "#{attr} can not be blank?" if attr.blank? | |
end | |
def get_email_validation(attr) | |
return "#{attr} can not be blank?" if attr.valid? #copy this valid function to regex email validation | |
end | |
def get_zip_validation(attr) | |
return "#{attr} can not be blank?" if attr.valid? #must be numeric and min 5 digit long | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment