Module: Sinatra::ParamValidator::Validator::Form::Helpers

Defined in:
lib/sinatra/param_validator/validator/form.rb

Overview

Helpers for Sinatra templates

Instance Method Summary collapse

Instance Method Details

#form_error?(field = nil) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
# File 'lib/sinatra/param_validator/validator/form.rb', line 19

def form_error?(field = nil)
  return !flash[:form_errors].nil? && !flash[:form_errors]&.empty? if field.nil?

  (flash[:form_errors] || {}).key?(field)
end

#form_errors(field) ⇒ Object



25
26
27
# File 'lib/sinatra/param_validator/validator/form.rb', line 25

def form_errors(field)
  (flash[:form_errors] || {}).fetch(field, [])
end

#form_value(field) ⇒ Object



15
16
17
# File 'lib/sinatra/param_validator/validator/form.rb', line 15

def form_value(field)
  flash[:params]&.fetch(field, nil)
end

#form_values(hash) ⇒ Object



10
11
12
13
# File 'lib/sinatra/param_validator/validator/form.rb', line 10

def form_values(hash)
  hash = IndifferentHash[hash]
  flash.now[:params] = flash.now.key?(:params) ? hash.merge(flash.now[:params]) : hash
end

#invalid_feedback(field, default = nil) ⇒ Object



29
30
31
32
# File 'lib/sinatra/param_validator/validator/form.rb', line 29

def invalid_feedback(field, default = nil)
  fields = Array(field)
  fields.any? { |f| form_error? f } ? fields.map { |f| form_errors f }.flatten.join('<br />') : default
end