
- Learning Ruby on Rails
- Rails 2.1 Home
- Rails 2.1 Introduction
- Rails 2.1 Installation
- Rails 2.1 Framework
- Rails 2.1 Dir Structure
- Rails 2.1 Examples
- Rails 2.1 Database Setup
- Rails 2.1 Active Records
- Rails 2.1 Migrations
- Rails 2.1 Controllers
- Rails 2.1 Views
- Rails 2.1 Layouts
- Rails 2.1 Scaffolding
- Rails 2.1 and AJAX
- Rails 2.1 Uploads Files
- Rails 2.1 Sends Emails
- Advanced Ruby on Rails 2.1
- Rails 2.1 RMagick Guide
- Rails 2.1 Basic HTTP Auth
- Rails 2.1 Error Handling
- Rails 2.1 Routes System
- Rails 2.1 Unit Testing
- Advanced Ruby on Rails 2.1
- Rails 2.1 Tips & Tricks
- Quick Reference Guide
- Quick Reference Guide
- Ruby on Rails 2.1 Useful Resources
- Ruby on Rails 2.1 - Resources
- Ruby on Rails 2.1 - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Ruby on Rails 2.1 - User Input Validations
Here is a list of validations that you can perform on a user input −
validates_presence_of
The following code checks that the last_name and the first_name contain data and are not NULL.
validates_presence_of :firstname, :lastname
validates_length_of
The following example shows various validations on a single file. These validations can be performed separately.
validates_length_of :password, :minimum => 8 # more than 8 characters :maximum => 16 # shorter than 16 characters :in => 8..16 # between 8 and 16 characters :too_short => 'way too short' :too_long => 'way to long'
validates_acceptance_of
The following code will accept only 'Y' value for an option field.
validates_acceptance_of :option :accept => 'Y'
validates_confirmation_of
The fields password and password_confirmation must match and will be used as follows −
validates_confirmation_of :password
validates_uniqueness_of
The following code puts a condition for user_name to be unique.
validates_uniqueness_of :user_name
validates_format_of
The following code validates that a given email ID is in a valid format. It shows how you can use a regular expression to validate a field.
validates_format_of :email :with => /^(+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
validates_numericality_of
It validates that a given field is numeric.
validates_numericality_of :value :only_integer => true :allow_nil => true
validates_inclusion_of
The following code checks that the passed value is an enumeration and falls in the given range.
validates_inclusion_of :gender, :in => %w( m, f )
validates_exclusion_of
The following code checks that the given values do not fall in the given range.
validates_exclusion_of :age :in => 13..19
validates_inclusion_of
The following code checks that the given values fall in the given range. This is the opposite of validates_exclusion_of.
validates_inclusion_of :age :in => 13..19
validates_associated
It validates that the associated object is valid.
Options for all Validations
You can use the following options along with all the validations.
:message => 'my own errormessage' Use this to print a custom error message in case of validation fails.
:on => :create or :update This will be used in such cases where you want to perform validation only when record is being created or updated. If you use :create, then this validation works only when there is a create operation on database.