Class: Formeze::Validation
- Inherits:
-
Object
- Object
- Formeze::Validation
- Defined in:
- lib/formeze.rb
Instance Method Summary collapse
- #error_key ⇒ Object
- #error_message ⇒ Object
-
#initialize(field, options, &block) ⇒ Validation
constructor
A new instance of Validation.
- #validate(form) ⇒ Object
- #validates?(form) ⇒ Boolean
Constructor Details
#initialize(field, options, &block) ⇒ Validation
Returns a new instance of Validation.
129 130 131 |
# File 'lib/formeze.rb', line 129 def initialize(field, , &block) @field, , @block = field, , block end |
Instance Method Details
#error_key ⇒ Object
133 134 135 |
# File 'lib/formeze.rb', line 133 def error_key .fetch(:error) { :invalid } end |
#error_message ⇒ Object
137 138 139 |
# File 'lib/formeze.rb', line 137 def Formeze.translate(error_key, :scope => [:formeze, :errors], :default => 'is invalid') end |
#validate(form) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/formeze.rb', line 145 def validate(form) if validates?(form) && !form.errors_on?(@field.name) return_value = if @block.arity == 1 @block.call(form.send(@field.name)) else form.instance_eval(&@block) end form.add_error(@field, ) unless return_value end end |
#validates?(form) ⇒ Boolean
141 142 143 |
# File 'lib/formeze.rb', line 141 def validates?(form) .has_key?(:when) ? form.instance_eval(&[:when]) : true end |