Class: Brut::FrontEnd::Forms::ValidityState
- Inherits:
-
Object
- Object
- Brut::FrontEnd::Forms::ValidityState
- Includes:
- Enumerable
- Defined in:
- lib/brut/front_end/forms/validity_state.rb
Overview
Mirrors a web browser's ValidityState API, but can also capture additional arbitrary server-side constraint violations to create an entire picture of all constraints violated by a given form input. In a sense, this is a wrapper for one or more ConstraintViolation instances in the context of an input.
This class also holds the logic related to client- vs. server-side constraint
violations. As such, KEYS is the list of known client-side
constraint violation keys, as defined by browser's ValidityState
class. These
are left as camel-case to make this clear.
Constant Summary collapse
- KEYS =
These are the attributes of the browser's
ValidityState
's properties. They are left as camel-case to clearly indicate they come from the browser. [ "badInput", "customError", "patternMismatch", "rangeOverflow", "rangeUnderflow", "stepMismatch", "tooLong", "tooShort", "typeMismatch", "valueMissing", ]
Instance Method Summary collapse
-
#constraint_violations? ⇒ Boolean
Returns true if there are constraint violations.
-
#each {|constraint| ... } ⇒ Object
Iterate over each constraint violation.
-
#initialize(constraint_violations = {}) ⇒ ValidityState
constructor
Create a validity state initialized with the given violations.
-
#server_side_constraint_violation(key:, context:) ⇒ Object
Set a server-side constraint violation.
-
#valid? ⇒ Boolean
Returns true if there are no constraint violations.
Constructor Details
#initialize(constraint_violations = {}) ⇒ ValidityState
Create a validity state initialized with the given violations
constraint violation described by the key. The keys are i18n fragments used to construct error messages.
19 20 21 22 23 24 25 26 27 |
# File 'lib/brut/front_end/forms/validity_state.rb', line 19 def initialize(constraint_violations={}) @constraint_violations = constraint_violations.map { |key,is_violation| if is_violation Brut::FrontEnd::Forms::ConstraintViolation.new(key: key, context: {}) else nil end }.compact end |
Instance Method Details
#constraint_violations? ⇒ Boolean
Returns true if there are constraint violations
33 |
# File 'lib/brut/front_end/forms/validity_state.rb', line 33 def constraint_violations? = !self.valid? |
#each {|constraint| ... } ⇒ Object
Iterate over each constraint violation
48 49 50 51 52 |
# File 'lib/brut/front_end/forms/validity_state.rb', line 48 def each(&block) @constraint_violations.each do |constraint| block.call(constraint) end end |
#server_side_constraint_violation(key:, context:) ⇒ Object
Set a server-side constraint violation. This is essentially arbitrary and dependent on your use-case.
40 41 42 |
# File 'lib/brut/front_end/forms/validity_state.rb', line 40 def server_side_constraint_violation(key:,context:) @constraint_violations << Brut::FrontEnd::Forms::ConstraintViolation.new(key: key, context: context, server_side: true) end |
#valid? ⇒ Boolean
Returns true if there are no constraint violations
30 |
# File 'lib/brut/front_end/forms/validity_state.rb', line 30 def valid? = @constraint_violations.empty? |