Module: Wedge::Plugins::Form::Validations

Includes:
Methods
Included in:
Wedge::Plugins::Form
Defined in:
lib/wedge/plugins/form/validations.rb

Overview

Provides a base implementation for extensible validation routines. Scrivener::Validations currently only provides the following assertions:

  • assert

  • assert_present

  • assert_format

  • assert_numeric

  • assert_url

  • assert_email

  • assert_member

  • assert_length

  • assert_decimal

  • assert_equal

The core tenets that Scrivener::Validations advocates can be summed up in a few bullet points:

  1. Validations are much simpler and better done using composition rather than macros.

  2. Error messages should be kept separate and possibly in the view or presenter layer.

  3. It should be easy to write your own validation routine.

Other validations are simply added on a per-model or per-project basis.

Examples:


class Quote
  attr_accessor :title
  attr_accessor :price
  attr_accessor :date

  def validate
    assert_present :title
    assert_numeric :price
    assert_format  :date, /\A[\d]{4}-[\d]{1,2}-[\d]{1,2}\z
  end
end

s = Quote.new
s.valid?
# => false

s.errors
# => { :title => [:not_present],
       :price => [:not_numeric],
       :date  => [:format] }

Instance Method Summary collapse

Methods included from Methods

#client?, included, #server?

Instance Method Details

#_errorsObject

Hash of errors for each attribute in this model.



102
103
104
105
106
107
108
# File 'lib/wedge/plugins/form/validations.rb', line 102

def _errors
  @_errors ||= Hash.new do |hash, key|
    data = _accessor_options[key] && _accessor_options[key].key?(:form) ? {} : []
    alias_key       = _aliases[key] || key
   _model_errors[alias_key] = hash[key] = data
  end
end

#_model_errorsObject



110
111
112
# File 'lib/wedge/plugins/form/validations.rb', line 110

def _model_errors
  @_model_errors ||= {}
end

#error(key, value) ⇒ Object



87
88
89
90
# File 'lib/wedge/plugins/form/validations.rb', line 87

def error key, value
  value = [value] unless value.is_a? Array
  _errors[key] = value
end

#errorsObject



92
93
94
# File 'lib/wedge/plugins/form/validations.rb', line 92

def errors
  IndifferentHash.new(_errors)
end

#model_errorsObject

gives back errors using the model_alias keys



97
98
99
# File 'lib/wedge/plugins/form/validations.rb', line 97

def model_errors
  IndifferentHash.new(_model_errors)
end

#valid(atts) ⇒ Object



78
79
80
81
# File 'lib/wedge/plugins/form/validations.rb', line 78

def valid atts
  _set_atts atts
  valid?
end

#valid?Boolean

Check if the current model state is valid. Each call to #valid? will reset the #errors array.

All validations should be declared in a ‘validate` method.

Examples:


class Login
  attr_accessor :username
  attr_accessor :password

  def validate
    assert_present :user
    assert_present :password
  end
end

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/wedge/plugins/form/validations.rb', line 72

def valid?
  _errors.clear
  validate
  _errors.empty?
end

#validateObject

Base validate implementation. Override this method in subclasses.



84
85
# File 'lib/wedge/plugins/form/validations.rb', line 84

def validate
end