Implements a meta-language for validating managed objects. Typically used in Validator objects but can be included in managed objects too.
Additional og-related validation macros can be found in lib/og/validation.
The following validation macros are available:
* validate_value * validate_confirmation * validate_format * validate_length * validate_inclusion
Og/Database specific validation methods are added in the file og/validation.rb
Example
class User
attr_accessor :name, String attr_accessor :level, Fixnum validate_length :name, :range => 2..6 validate_unique :name, :msg => :name_allready_exists validate_format :name, :format => /[a-z]*/, :msg => 'invalid format', :on => :create end class CustomUserValidator include Validation validate_length :name, :range => 2..6, :msg_short => :name_too_short, :msg_long => :name_too_long end user = @request.fill(User.new) user.level = 15 unless user.valid? user.save else p user.errors[:name] end unless user.save p user.errors.on(:name) end unless errors = CustomUserValidator.errors(user) user.save else p errors[:name] end
Methods
Classes and Modules
Module Glue::Validation::ClassMethodsClass Glue::Validation::Errors
Class Glue::Validation::Key
Attributes
| [RW] | errors | If the validate method returns true, this attributes holds the errors found. |
Public Instance methods
Call the #validate method for this object. If validation errors are found, sets the @errors attribute to the Errors object and returns true.
[ show source ]
# File lib/glue/validation.rb, line 157
157: def valid?
158: validate
159: @errors.empty?
160: end
Evaluate the class and see if it is valid. Can accept any parameter for ‘on’ event, and defaults to :save
[ show source ]
# File lib/glue/validation.rb, line 166
166: def validate(on = :save)
167: @errors = Errors.new
168:
169: return if self.class.validations.length == 0
170:
171: for event, block in self.class.validations
172: block.call(self) if event == on.to_sym
173: end
174: end