Created
March 16, 2011 13:38
-
-
Save konung/872500 to your computer and use it in GitHub Desktop.
This file contains 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 CreateContactAttempts < ActiveRecord::Migration | |
def self.up | |
create_table :contact_attempts do |t| | |
t.string :first_name | |
t.string :last_name | |
t.string :email | |
t.string :url | |
t.string :company | |
t.text :message_body | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :contact_attempts | |
end | |
end |
This file contains 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
= form_for contact_attempt , :validate => true do |f| | |
-if contact_attempt.errors.any? | |
#error_explanation | |
%h2= "#{pluralize(contact_attempt.errors.count, "error")} prohibited this contact_attempt from being saved:" | |
%ul | |
- contact_attempt.errors.full_messages.each do |msg| | |
%li= msg | |
.field | |
= f.label :first_name | |
= f.text_field :first_name | |
%label.input-hint John | |
.field | |
= f.label :last_name | |
= f.text_field :last_name | |
%label.input-hint Doe | |
.field | |
= f.label :email | |
= f.text_field :email | |
%label.input-hint [email protected] | |
.field | |
= f.label :url | |
= f.text_field :url | |
%label.input-hint www.example.com | |
.field | |
= f.label :company | |
= f.text_field :company | |
%label.input-hint Example Inc. | |
.field | |
= f.label :message_body | |
= f.text_area :message_body | |
%label.input-hint Leave your comments | |
.actions | |
= f.submit 'Send' , :class => "awesome large" |
This file contains 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
// public/javascripts/client-side-custom-validators.js | |
// Note: this style of how to write the custom validators for the client side will likely change over time. | |
// Refer back to this wiki in the future. | |
// The validator variable is a JSON Object | |
// The selector variable is a jQuery Object | |
clientSideValidations.validator['email'] = function(validator, selector) { | |
// Your validator code goes in here | |
if (!/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i.test(selector.val())) { | |
// When the value fails to pass validation you need to return the error message. | |
// It can be derived from validator.message | |
return validator.message; | |
} | |
} | |
clientSideValidations.validator['url'] = function(validator, selector) { | |
// Your validator code goes in here | |
if (!/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/i.test(selector.val())) { | |
// When the value fails to pass validation you need to return the error message. | |
// It can be derived from validator.message | |
return validator.message; | |
} | |
} | |
This file contains 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 ContactAttempt < ActiveRecord::Base | |
validates :first_name, :presence => true, :length => {:minimum => 2, :maximum => 15}, :alphanumeric_format => true | |
validates :last_name, :presence => true, :length => {:minimum => 2, :maximum => 20}, :alphanumeric_format => true | |
validates_email :email, :presence => true, :length => {:minimum => 5, :maximum => 50} | |
validates_url :url,:presence => false, :length => {:maximum => 50} | |
validate :company, :length => {:maximum => 50} | |
validates :message_body, :length => {:minimum => 5, :maximum => 500} | |
end |
This file contains 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 UrlValidator < ActiveModel::EachValidator | |
def validate_each(record, attr_name, value) | |
unless value =~ /[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix | |
record.errors.add(attr_name, :url, options.merge(:value => value)) | |
end | |
end | |
end | |
# This allows us to assign the validator in the model | |
module ActiveModel::Validations::HelperMethods | |
def validates_url(*attr_names) | |
validates_with UrlValidator, _merge_attributes(attr_names) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment