Created
November 14, 2012 03:12
-
-
Save flakyfilibuster/4070029 to your computer and use it in GitHub Desktop.
Validations in Rails
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
# Validate that the price contains a maximum of two decimal places | |
# Validate the image url is a proper URL and ends with a image file type | |
# Validate that the size is only one of the following: "small", "medium", "large" | |
validates :price, :format => { :with => /^\d+??(?:\.\d{0,2})?$/ } | |
validates :imageurl, :format => { :with => /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\.(png|jpg|gif)?$/ } | |
validates :size, :inclusion => { :in => %w(small medium large) } |
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
# Validate the presence of the title, price, description, image url, size | |
# Validate that the title is unique, and less than 50 characters | |
# Validate that the description is less than 250 words | |
validates :title, :price, :description, :imageurl, :size, :presence => true | |
validates :title, :uniqueness => true, :length => { :minimum => 50 } | |
validates :description, :length => { :maximum => 250 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment