Class: Brut::FrontEnd::Forms::ConstraintViolation

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/front_end/forms/constraint_violation.rb

Overview

Represents a specific error with a field. Essentially wraps an i18n key fragment and interpolated values for use with other form-related classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, context:, server_side: :based_on_key) ⇒ ConstraintViolation

Create a constraint violations

If true, #client_side? will return false no matter what.

Parameters:

  • key (String|Symbol)

    I18n key fragment representing this violation.

  • context (Hash|nil)

    interpolated values useful in rendering the message

  • server_side (true|:based_on_key) (defaults to: :based_on_key)

    If :based_on_key, #client_side? will return true if key is in ValidityState::KEYS.



16
17
18
19
20
21
22
23
# File 'lib/brut/front_end/forms/constraint_violation.rb', line 16

def initialize(key:,context:, server_side: :based_on_key)
  @key = key.to_s
  @client_side = Brut::FrontEnd::Forms::ValidityState::KEYS.include?(@key) && server_side != true
  @context = context || {}
  if !@context.kind_of?(Hash)
    raise "#{self.class} created for key #{key} with an invalid context: '#{context}/#{context.class}'. Context must be nil or a hash"
  end
end

Instance Attribute Details

#contextHash (readonly)

Returns interpolated values useful in rendering the actual message.

Returns:

  • (Hash)

    interpolated values useful in rendering the actual message



8
9
10
# File 'lib/brut/front_end/forms/constraint_violation.rb', line 8

def context
  @context
end

#keyString (readonly)

Returns the key fragment representing the violation.

Returns:

  • (String)

    the key fragment representing the violation



6
7
8
# File 'lib/brut/front_end/forms/constraint_violation.rb', line 6

def key
  @key
end

Instance Method Details

#client_side?true|false

Returns true if this violation is a client-side violation.

Returns:

  • (true|false)

    true if this violation is a client-side violation



26
# File 'lib/brut/front_end/forms/constraint_violation.rb', line 26

def client_side? = @client_side

#to_sObject



27
# File 'lib/brut/front_end/forms/constraint_violation.rb', line 27

def to_s = @key