Class: Brut::BackEnd::Validators::FormValidator
- Inherits:
-
Object
- Object
- Brut::BackEnd::Validators::FormValidator
- 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.
Class Method Summary collapse
-
.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.
Instance Method Summary collapse
-
#validate(form) ⇒ Object
Validate the given form, calling FrontEnd::Form#server_side_constraint_violation on each validate failure found.
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
28 29 30 31 |
# File 'lib/brut/back_end/validators/form_validator.rb', line 28 def self.validate(input_name,) @@validations ||= {} @@validations[input_name] = end |
Instance Method Details
#validate(form) ⇒ Object
Validate the given form, calling FrontEnd::Form#server_side_constraint_violation on each validate failure found.
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,| value = form.send(attribute) .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 |