module BootstrapForm::Components::Validation

Private Instance Methods

error?(name) click to toggle source
# File lib/bootstrap_form/components/validation.rb, line 10
def error?(name)
  object.respond_to?(:errors) && !(name.nil? || object.errors[name].empty?)
end
generate_error(name) click to toggle source
# File lib/bootstrap_form/components/validation.rb, line 46
def generate_error(name)
  return unless inline_error?(name)

  help_text = get_error_messages(name)
  help_klass = "invalid-feedback"
  help_tag = :div

  content_tag(help_tag, help_text, class: help_klass)
end
get_error_messages(name) click to toggle source
# File lib/bootstrap_form/components/validation.rb, line 56
def get_error_messages(name)
  object.errors[name].join(", ")
end
inline_error?(name) click to toggle source
# File lib/bootstrap_form/components/validation.rb, line 42
def inline_error?(name)
  error?(name) && inline_errors
end
presence_validator?(target_validators) click to toggle source
# File lib/bootstrap_form/components/validation.rb, line 28
def presence_validator?(target_validators)
  has_presence_validator = target_validators.include?(
    ActiveModel::Validations::PresenceValidator
  )

  if defined? ActiveRecord::Validations::PresenceValidator
    has_presence_validator |= target_validators.include?(
      ActiveRecord::Validations::PresenceValidator
    )
  end

  has_presence_validator
end
required_attribute?(obj, attribute) click to toggle source
# File lib/bootstrap_form/components/validation.rb, line 14
def required_attribute?(obj, attribute)
  return false unless obj && attribute

  target = obj.class == Class ? obj : obj.class

  target_validators = if target.respond_to? :validators_on
                        target.validators_on(attribute).map(&:class)
                      else
                        []
                      end

  presence_validator?(target_validators)
end