Class: Brut::BackEnd::Validators::FormValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/back_end/validators/form_validator.rb

Overview

Provides a very light DSL to declaring server-side validations for your FrontEnd::Form subclass. Unlike Active Record, these validations aren't mixed-into another object. Your subclass of this class is a standalone object that will operate on a form.

Examples:

# Suppose you are creating a widget with a name and description.
# The form requires name, but not description, however if
# the user initiates a "publish" action from that form, the description is
# required. This cannot be managed with HTML alone.
class WidgetPublishValidator < Brut::BackEnd::Validators::FormValidator
  validate :description, required: true, minlength: 10
end

# Then, in your back-end logic somewhere

validator = WidgetPublishValidator.new
validator.validate(form)
if form.constraint_violations?
  # return back to the user
else
  # proceed with business logic
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validate(input_name, options) ⇒ Object

Called inside the subclass body to indicate that a given form input should be validated based on the given options

Parameters:

  • input_name (String|Symbol)

    name of the input of the form

  • options (Hash)

    options describing the validation to perform.



28
29
30
31
# File 'lib/brut/back_end/validators/form_validator.rb', line 28

def self.validate(input_name,options)
  @@validations ||= {}
  @@validations[input_name] = options
end

Instance Method Details

#validate(form) ⇒ Object

Validate the given form, calling FrontEnd::Form#server_side_constraint_violation on each validate failure found.

Parameters:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/brut/back_end/validators/form_validator.rb', line 36

def validate(form)
  @@validations.each do |attribute,options|
    value = form.send(attribute)
    options.each do |option, option_value|
      case option
      when :required
        if option_value == true
          if value.to_s.strip == ""
            form.server_side_constraint_violation(input_name: attribute, key: :required)
          end
        end
      when :minlength
        if value.respond_to?(:length) || value.nil?
          if value.nil? || value.length < option_value
            form.server_side_constraint_violation(input_name: attribute, key: :too_short, context: { minlength: option_value })
          end
        else
          raise "'#{attribute}''s value (a '#{value.class}') does not respond to 'length' - :minlength cannot be used as a validation"
        end
      else
        raise "'#{option}' is not a recognized validation option"
      end
    end
  end
end