phlex-forms 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +38 -0
- data/README.md +22 -1
- data/app/javascript/phlex_forms/controllers/validations/base_controller.js +5 -5
- data/app/javascript/phlex_forms/controllers/validations/form_controller.js +3 -3
- data/app/javascript/phlex_forms/controllers/validations/length_controller.js +3 -3
- data/lib/forms/checkbox_group.rb +85 -0
- data/lib/forms/field.rb +33 -1
- data/lib/forms/form.rb +26 -2
- data/lib/forms/plain/checkbox_group.rb +27 -0
- data/lib/forms/validations/introspector.rb +9 -5
- data/lib/phlex_forms/builder.rb +11 -0
- data/lib/phlex_forms/theme.rb +2 -0
- data/lib/phlex_forms/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 74bd32547d30535c35060ea6c93f9cfd199614a9ced2cf1a95a7c32fe62eb3e6
|
|
4
|
+
data.tar.gz: cf0087d9bee7cb13366ef489fb8764c82867b406881934eb92cdb7dbc271d0f8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 33980ae64283b549a82ab25bd445df927616ac7bc5d0929c41aba13c10439d795c631268e6154bd64a5c0b9783baee4573e7e4efdeef7ecd3e8ac4633791b1bd
|
|
7
|
+
data.tar.gz: d4d4efd19dd0dbde90311922d91040052925a87948cdcd95d42b50049028186a6cd1b7b80a8b7027154cadd67e8801ad242777817a1ab214d13088e40a5558b3
|
data/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,44 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
9
9
|
|
|
10
10
|
### Added
|
|
11
11
|
|
|
12
|
+
- **`checkbox_group` — batched checkbox group for array-valued fields** (the
|
|
13
|
+
tag/facet-picker shape): `f.checkbox_group(:tag_ids, Tag.all, value: :id,
|
|
14
|
+
label: :name, variant: :pill, size: :sm)`, or via field inference
|
|
15
|
+
(`f.field :tag_ids, as: :checkbox_group, collection: Tag.all, value: :id`).
|
|
16
|
+
Shares one array-valued field name with a leading empty-array hidden field,
|
|
17
|
+
derives the checked set from the model's current value, and renders under both
|
|
18
|
+
themes. `variant:` (`:stack`/`:inline`/`:pill`) is layout-only, no JS.
|
|
19
|
+
|
|
20
|
+
### Changed
|
|
21
|
+
|
|
22
|
+
- **Client-side validation Stimulus identifiers dropped the `forms--` prefix**:
|
|
23
|
+
the bundled controllers now emit `validations--presence`, `validations--length`,
|
|
24
|
+
… (and the `validations--form` coordinator) so
|
|
25
|
+
`lazyLoadControllersFrom("phlex_forms/controllers")` resolves them to their
|
|
26
|
+
shipped path `phlex_forms/controllers/validations/*_controller` — previously
|
|
27
|
+
`forms--validations--*` derived `.../forms/validations/*`, which 404'd and the
|
|
28
|
+
controllers never connected (issue #12). The `data-validations--*` binding
|
|
29
|
+
attributes and the `invalidate:validations` event changed to match. Hosts that
|
|
30
|
+
registered `forms--validations--*` explicitly must update the identifier.
|
|
31
|
+
|
|
32
|
+
### Fixed
|
|
33
|
+
|
|
34
|
+
- **`f.Radio` / `Field#radio` rendered the model's current value on every radio
|
|
35
|
+
instead of each radio's own value**: `field_attributes` carried `value:
|
|
36
|
+
field_value` and was splatted after the explicit positional value, clobbering
|
|
37
|
+
it — a new record lost the value entirely, an edit form gave every radio the
|
|
38
|
+
same value. `radio` now drops `field_attributes`' `value` (issue #13).
|
|
39
|
+
- **`Form(validate: true)` never fired client-side validation on submit**: the
|
|
40
|
+
coordinator controller was attached but no `data-action` wired its `onSubmit`
|
|
41
|
+
handler, so submitting an invalid form was not blocked. `apply_validation_coordinator`
|
|
42
|
+
now emits `submit->validations--form#onSubmit` (joined with any
|
|
43
|
+
caller-supplied `data-action`).
|
|
44
|
+
- **`fields_for` iterated a Hash-backed association (JSONB), emitting bogus
|
|
45
|
+
indices**: a Hash responds to `#each_with_index`, so a JSONB column rendered
|
|
46
|
+
with `nested_attributes: false` produced `scope[assoc][0][field]`, `[1]`, …
|
|
47
|
+
instead of a single `scope[assoc][field]`. It is now treated as a single
|
|
48
|
+
nested scope; only genuine collections (Enumerable, not Hash) iterate.
|
|
49
|
+
|
|
12
50
|
- **`Forms::Base` declarative form classes**: subclass, declare fields in
|
|
13
51
|
`#fields` where `self` IS the form (bare `field :email`, no `f.` prefix),
|
|
14
52
|
render with `render UserForm.new(model: @user)`. Class-level `form_options`
|
data/README.md
CHANGED
|
@@ -337,6 +337,11 @@ import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
|
|
|
337
337
|
lazyLoadControllersFrom("phlex_forms/controllers", application)
|
|
338
338
|
```
|
|
339
339
|
|
|
340
|
+
The emitted identifiers are `validations--presence`, `validations--length`, … (and
|
|
341
|
+
the form-level `validations--form` coordinator), which `lazyLoadControllersFrom`
|
|
342
|
+
resolves to `phlex_forms/controllers/validations/*_controller` — the path the gem
|
|
343
|
+
ships them at.
|
|
344
|
+
|
|
340
345
|
Messages ship for `en` / `fr` / `af`; override via `window.PhlexForms.messages`.
|
|
341
346
|
|
|
342
347
|
## Nested attributes, collections & escape valves
|
|
@@ -351,13 +356,29 @@ f.fields_for(:settings, nested_attributes: false) do |s|
|
|
|
351
356
|
end
|
|
352
357
|
|
|
353
358
|
f.collection_check_boxes(:role_ids, Role.all, :id, :name) do |b|
|
|
354
|
-
render b.check_box
|
|
359
|
+
render b.check_box # per-item control, full custom layout
|
|
355
360
|
render b.label
|
|
356
361
|
end
|
|
357
362
|
|
|
363
|
+
# The batched "tag/facet picker" shape: one array-valued field name, checked
|
|
364
|
+
# state derived from the model (record.tag_ids), sensible defaults, no block.
|
|
365
|
+
f.checkbox_group(:tag_ids, Tag.all, value: :id, label: :name)
|
|
366
|
+
f.checkbox_group(:tag_ids, Tag.all, value: :id,
|
|
367
|
+
label: ->(t) { t.name.presence || t.slug }, # Symbol method or Proc
|
|
368
|
+
variant: :pill, # :stack (default) | :inline | :pill
|
|
369
|
+
size: :sm) # daisyUI checkbox size
|
|
370
|
+
# ...or through field inference:
|
|
371
|
+
f.field :tag_ids, as: :checkbox_group, collection: Tag.all, value: :id, label: :name
|
|
372
|
+
|
|
358
373
|
f.collection_select(:country_id, Country.all, :id, :name, prompt: "Select…")
|
|
359
374
|
```
|
|
360
375
|
|
|
376
|
+
`checkbox_group` submits an array param (`user[tag_ids][]`) with a leading
|
|
377
|
+
empty-array hidden field, so deselecting everything still submits. The checked
|
|
378
|
+
set comes from the model's current value matched by each item's resolved
|
|
379
|
+
`value:` — re-rendering an edit form pre-checks the right boxes. The `:pill`
|
|
380
|
+
variant styles the active chip with Tailwind's `has-[:checked]:` (no JS).
|
|
381
|
+
|
|
361
382
|
`Form(model: @item, scope: false)` emits **bare** field names
|
|
362
383
|
(`name="quantity"`) — the shape phlex-reactive row editors and
|
|
363
384
|
`<template>`-cloned rows need. External widgets bind through the public
|
|
@@ -10,7 +10,7 @@ import { Controller } from "@hotwired/stimulus"
|
|
|
10
10
|
// reads from data attributes; the base class only knows about the
|
|
11
11
|
// `allowBlank` / `allowNil` short-circuits.
|
|
12
12
|
export class FieldValidatorController extends Controller {
|
|
13
|
-
// `error` is opt-in: callers that pre-render a `<p data-
|
|
13
|
+
// `error` is opt-in: callers that pre-render a `<p data-validations--<validator>-target="error">`
|
|
14
14
|
// get a stable slot the controller toggles. Inputs without an
|
|
15
15
|
// explicit target still work — the controller lazily creates one
|
|
16
16
|
// adjacent to the input below.
|
|
@@ -24,12 +24,12 @@ export class FieldValidatorController extends Controller {
|
|
|
24
24
|
// directly to <input>, <textarea>, <select> via the form builder.
|
|
25
25
|
connect() {
|
|
26
26
|
this.element.addEventListener("blur", this.onBlur)
|
|
27
|
-
this.element.addEventListener("invalidate:
|
|
27
|
+
this.element.addEventListener("invalidate:validations", this.onValidate)
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
disconnect() {
|
|
31
31
|
this.element.removeEventListener("blur", this.onBlur)
|
|
32
|
-
this.element.removeEventListener("invalidate:
|
|
32
|
+
this.element.removeEventListener("invalidate:validations", this.onValidate)
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
onBlur = () => {
|
|
@@ -150,7 +150,7 @@ export class FieldValidatorController extends Controller {
|
|
|
150
150
|
// adjacent to the input. Keeps the framework usable on plain
|
|
151
151
|
// forms that haven't opted into the static-target convention.
|
|
152
152
|
const id = this.element.id || this.element.name
|
|
153
|
-
const selector = `[data-
|
|
153
|
+
const selector = `[data-validations--error="${id}"]`
|
|
154
154
|
const existing = this.element.closest("form")?.querySelector(selector)
|
|
155
155
|
if (existing) return existing
|
|
156
156
|
if (!create) return null
|
|
@@ -159,7 +159,7 @@ export class FieldValidatorController extends Controller {
|
|
|
159
159
|
container.className = "text-error text-sm mt-1"
|
|
160
160
|
// `dataset` rejects keys with `--`, so we set the attribute
|
|
161
161
|
// directly. The CSS selector still matches.
|
|
162
|
-
container.setAttribute("data-
|
|
162
|
+
container.setAttribute("data-validations--error", id)
|
|
163
163
|
this.element.insertAdjacentElement("afterend", container)
|
|
164
164
|
return container
|
|
165
165
|
}
|
|
@@ -3,7 +3,7 @@ import { Controller } from "@hotwired/stimulus"
|
|
|
3
3
|
// Form-level coordinator for the validation framework. Sits on the
|
|
4
4
|
// <form> element and intercepts `submit` to broadcast a synchronous
|
|
5
5
|
// validation event to every field. Each field controller listens
|
|
6
|
-
// for `invalidate:
|
|
6
|
+
// for `invalidate:validations`, runs its check, and (on
|
|
7
7
|
// failure) appends its error to the event's `detail.errors` array.
|
|
8
8
|
// If anything ended up in that array, we cancel the submit and
|
|
9
9
|
// focus the first invalid field.
|
|
@@ -23,11 +23,11 @@ export default class extends Controller {
|
|
|
23
23
|
fields.forEach((field) => {
|
|
24
24
|
const validators = (field.dataset.controller || "")
|
|
25
25
|
.split(/\s+/)
|
|
26
|
-
.filter((c) => c.startsWith("
|
|
26
|
+
.filter((c) => c.startsWith("validations--") && c !== "validations--form")
|
|
27
27
|
if (validators.length === 0) return
|
|
28
28
|
|
|
29
29
|
field.dispatchEvent(
|
|
30
|
-
new CustomEvent("invalidate:
|
|
30
|
+
new CustomEvent("invalidate:validations", {
|
|
31
31
|
detail: { errors },
|
|
32
32
|
}),
|
|
33
33
|
)
|
|
@@ -12,7 +12,7 @@ export default class extends FieldValidatorController {
|
|
|
12
12
|
// Stimulus walks the prototype chain to accumulate `static values`
|
|
13
13
|
// and `static targets`, so we only declare the validator-specific
|
|
14
14
|
// ones here. `counter` is opt-in: callers that pre-render
|
|
15
|
-
// `<span data-
|
|
15
|
+
// `<span data-validations--length-target="counter">` get a
|
|
16
16
|
// stable slot the controller updates. Inputs without an explicit
|
|
17
17
|
// target still get a lazily-injected one (see counterElement).
|
|
18
18
|
static targets = ["counter"]
|
|
@@ -80,7 +80,7 @@ export default class extends FieldValidatorController {
|
|
|
80
80
|
|
|
81
81
|
// Fallback: cache-by-id lookup or lazy injection for plain forms.
|
|
82
82
|
const id = this.element.id || this.element.name
|
|
83
|
-
const selector = `[data-
|
|
83
|
+
const selector = `[data-validations--counter="${id}"]`
|
|
84
84
|
const existing = this.element.closest("form")?.querySelector(selector)
|
|
85
85
|
if (existing) return existing
|
|
86
86
|
if (!create) return null
|
|
@@ -88,7 +88,7 @@ export default class extends FieldValidatorController {
|
|
|
88
88
|
const counter = document.createElement("span")
|
|
89
89
|
counter.className = "text-xs text-base-content/60 ml-auto"
|
|
90
90
|
// `dataset` rejects keys containing `--`; use the raw attribute API.
|
|
91
|
-
counter.setAttribute("data-
|
|
91
|
+
counter.setAttribute("data-validations--counter", id)
|
|
92
92
|
this.element.insertAdjacentElement("afterend", counter)
|
|
93
93
|
return counter
|
|
94
94
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Forms
|
|
4
|
+
# A batched checkbox group for an array-valued field (the tag/facet-picker
|
|
5
|
+
# shape). Renders a set of checkboxes sharing ONE array-valued field name
|
|
6
|
+
# (`user[tag_ids][]`), with a leading empty-array hidden field so an empty
|
|
7
|
+
# selection still submits, and derives each box's checked state from the
|
|
8
|
+
# resolved value: of its item against the model's current set.
|
|
9
|
+
#
|
|
10
|
+
# f.checkbox_group(:tag_ids, Tag.all, value: :id,
|
|
11
|
+
# label: ->(t) { t.name.presence || t.slug }, variant: :pill, size: :sm)
|
|
12
|
+
#
|
|
13
|
+
# value: method or proc -> the submitted value of each item (default :id)
|
|
14
|
+
# label: method or proc -> the visible text of each item (default :to_s)
|
|
15
|
+
# variant: :stack (default) | :inline | :pill — layout only, zero JS
|
|
16
|
+
# size: daisyUI checkbox size modifier (:xs :sm :md :lg :xl)
|
|
17
|
+
#
|
|
18
|
+
# The checked set is passed in pre-resolved by the builder (Field#checkbox_group
|
|
19
|
+
# matches the model's current value by each item's resolved value:), so the
|
|
20
|
+
# component itself stays presentation-only. Each checkbox's markup is delegated
|
|
21
|
+
# to DaisyUI::Checkbox so its size class is a literal, scanner-visible token.
|
|
22
|
+
class CheckboxGroup < Phlex::HTML
|
|
23
|
+
# variant -> the container class. The pill variant uses Tailwind's
|
|
24
|
+
# `has-[:checked]:` to style the active label with no JS.
|
|
25
|
+
VARIANT_CLASSES = {
|
|
26
|
+
stack: "flex flex-col gap-2",
|
|
27
|
+
inline: "flex flex-wrap gap-4",
|
|
28
|
+
pill: "flex flex-wrap gap-2"
|
|
29
|
+
}.freeze
|
|
30
|
+
|
|
31
|
+
def initialize(name:, id:, options:, variant: :stack, size: nil, error: false, **attributes)
|
|
32
|
+
@name = name # already the array name: "user[tag_ids][]"
|
|
33
|
+
@id = id
|
|
34
|
+
@options = options # [{ value:, label:, checked:, id: }, ...]
|
|
35
|
+
@variant = variant
|
|
36
|
+
@size = size
|
|
37
|
+
@error = error
|
|
38
|
+
@attributes = attributes
|
|
39
|
+
super()
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def view_template
|
|
43
|
+
# Empty-array hidden field so an empty selection still submits (the same
|
|
44
|
+
# convention as collection_check_boxes).
|
|
45
|
+
input(type: "hidden", name: @name, value: "")
|
|
46
|
+
|
|
47
|
+
div(class: group_classes, role: "group", "aria-invalid": @error || nil) do
|
|
48
|
+
@options.each { |option| item(option) }
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def item(option)
|
|
55
|
+
label(class: item_classes) do
|
|
56
|
+
render_checkbox(option)
|
|
57
|
+
span(class: item_label_classes) { option[:label].to_s }
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Delegate the checkbox markup to the daisyui gem so the size modifier
|
|
62
|
+
# resolves to a literal class (checkbox-sm, ...) the CSS scanner can see.
|
|
63
|
+
def render_checkbox(option)
|
|
64
|
+
render DaisyUI::Checkbox.new(
|
|
65
|
+
*checkbox_modifiers,
|
|
66
|
+
name: @name, id: option[:id], value: option[:value],
|
|
67
|
+
checked: option[:checked] || nil, class: @attributes[:class]
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def checkbox_modifiers = @size ? [@size] : []
|
|
72
|
+
|
|
73
|
+
# --- styling seams (the Plain twin overrides these to bare/empty) ---
|
|
74
|
+
|
|
75
|
+
def group_classes = VARIANT_CLASSES.fetch(@variant, VARIANT_CLASSES[:stack])
|
|
76
|
+
|
|
77
|
+
def item_classes
|
|
78
|
+
return "label cursor-pointer gap-2 justify-start" unless @variant == :pill
|
|
79
|
+
|
|
80
|
+
"badge badge-lg cursor-pointer gap-2 has-[:checked]:badge-primary"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def item_label_classes = nil
|
|
84
|
+
end
|
|
85
|
+
end
|
data/lib/forms/field.rb
CHANGED
|
@@ -67,11 +67,15 @@ module Forms
|
|
|
67
67
|
end
|
|
68
68
|
|
|
69
69
|
def radio(value, *modifiers, **options)
|
|
70
|
+
# field_attributes carries value: field_value (the model's CURRENT value).
|
|
71
|
+
# Drop it here so it can't clobber this radio's own positional value —
|
|
72
|
+
# otherwise every radio in the group renders the model's value (issue #13).
|
|
73
|
+
attrs = field_attributes.except(:value).merge(options)
|
|
70
74
|
theme[:radio].new(
|
|
71
75
|
*modifiers,
|
|
72
76
|
value:,
|
|
73
77
|
checked: field_value == value,
|
|
74
|
-
**
|
|
78
|
+
**attrs.merge(id: "#{field_id}_#{value}")
|
|
75
79
|
)
|
|
76
80
|
end
|
|
77
81
|
alias radio_button radio
|
|
@@ -102,6 +106,29 @@ module Forms
|
|
|
102
106
|
)
|
|
103
107
|
end
|
|
104
108
|
|
|
109
|
+
# A model-bound checkbox group over a collection. Shares one array-valued
|
|
110
|
+
# field name (`scope[name][]`) and derives the checked set from the model's
|
|
111
|
+
# current value, matched by each item's resolved value: (issue #9).
|
|
112
|
+
#
|
|
113
|
+
# field.checkbox_group(Tag.all, value: :id, label: ->(t) { t.name })
|
|
114
|
+
#
|
|
115
|
+
# value:/label: are a method name (Symbol) or a proc taking the item.
|
|
116
|
+
def checkbox_group(collection, value: :id, label: :to_s, **)
|
|
117
|
+
# The model's current value is already the raw values (e.g. record.tag_ids
|
|
118
|
+
# => [1, 3]), so compare against them directly — don't re-resolve value:.
|
|
119
|
+
selected = Array(field_value)
|
|
120
|
+
opts = Array(collection).map do |item|
|
|
121
|
+
item_value = resolve_item(item, value)
|
|
122
|
+
{
|
|
123
|
+
value: item_value,
|
|
124
|
+
label: resolve_item(item, label),
|
|
125
|
+
checked: selected.include?(item_value),
|
|
126
|
+
id: "#{field_id}_#{item_value}"
|
|
127
|
+
}
|
|
128
|
+
end
|
|
129
|
+
theme[:checkbox_group].new(name: "#{field_name}[]", id: field_id, options: opts, error: invalid?, **)
|
|
130
|
+
end
|
|
131
|
+
|
|
105
132
|
def label(text = nil, *modifiers, **, &block)
|
|
106
133
|
theme[:label].new(*modifiers, text: text || (block ? nil : field_label), for: field_id, **, &block)
|
|
107
134
|
end
|
|
@@ -229,6 +256,11 @@ module Forms
|
|
|
229
256
|
validator.options.key?(:if) || validator.options.key?(:unless) || validator.options.key?(:on)
|
|
230
257
|
end
|
|
231
258
|
|
|
259
|
+
# value:/label: for checkbox_group: a Proc taking the item, or a method name.
|
|
260
|
+
def resolve_item(item, accessor)
|
|
261
|
+
accessor.respond_to?(:call) ? accessor.call(item) : item.public_send(accessor)
|
|
262
|
+
end
|
|
263
|
+
|
|
232
264
|
def field_attributes
|
|
233
265
|
{ name: field_name, id: field_id, value: field_value, error: invalid? }
|
|
234
266
|
end
|
data/lib/forms/form.rb
CHANGED
|
@@ -104,7 +104,7 @@ module Forms
|
|
|
104
104
|
attributes_key = nested_attributes ? "#{association_name}_attributes" : association_name.to_s
|
|
105
105
|
base_scope = @scope ? "#{@scope}[#{attributes_key}]" : attributes_key
|
|
106
106
|
|
|
107
|
-
if
|
|
107
|
+
if collection?(associated)
|
|
108
108
|
associated.each_with_index do |item, index|
|
|
109
109
|
yield build_fields_for("#{base_scope}[#{index}]", item)
|
|
110
110
|
end
|
|
@@ -133,6 +133,13 @@ module Forms
|
|
|
133
133
|
end
|
|
134
134
|
end
|
|
135
135
|
|
|
136
|
+
# A batched checkbox group for an array-valued field (issue #9). Delegates to
|
|
137
|
+
# Field#checkbox_group, which derives the checked set from the model.
|
|
138
|
+
# f.checkbox_group(:tag_ids, Tag.all, value: :id, label: :name, variant: :pill)
|
|
139
|
+
def checkbox_group(name, collection, **)
|
|
140
|
+
render field_object(name).checkbox_group(collection, **)
|
|
141
|
+
end
|
|
142
|
+
|
|
136
143
|
# Rails-style collection_select over an enumerable of records.
|
|
137
144
|
def collection_select(name, collection, value_method, text_method, options = {}, html_options = {})
|
|
138
145
|
choices = collection.map do |item|
|
|
@@ -156,6 +163,15 @@ module Forms
|
|
|
156
163
|
|
|
157
164
|
private
|
|
158
165
|
|
|
166
|
+
# A genuine has_many collection (Array / ActiveRecord::Relation), NOT a
|
|
167
|
+
# Hash-backed nested scope. A Hash responds to #each_with_index but is a
|
|
168
|
+
# single nested record (a JSONB column), so iterating it would emit bogus
|
|
169
|
+
# positional indices — scope[assoc][0][field] — instead of scope[assoc][field]
|
|
170
|
+
# (issue #10). Enumerable-but-not-Hash covers Relations without requiring AR.
|
|
171
|
+
def collection?(associated)
|
|
172
|
+
associated.is_a?(Enumerable) && !associated.is_a?(Hash)
|
|
173
|
+
end
|
|
174
|
+
|
|
159
175
|
def build_fields_for(scope, item)
|
|
160
176
|
Forms::FieldsForBuilder.new(
|
|
161
177
|
model: item,
|
|
@@ -194,8 +210,16 @@ module Forms
|
|
|
194
210
|
# UI (novalidate) — the Stimulus layer owns error display.
|
|
195
211
|
def apply_validation_coordinator(attrs)
|
|
196
212
|
existing = attrs[:data][:controller].to_s
|
|
197
|
-
coordinator
|
|
213
|
+
# Derive the coordinator identifier from the introspector's prefix so the
|
|
214
|
+
# form-level and field-level controllers can never drift (issue #12).
|
|
215
|
+
coordinator = "#{Forms::Validations::Introspector::CONTROLLER_PREFIX}--form"
|
|
198
216
|
attrs[:data][:controller] = [existing, coordinator].reject(&:empty?).join(" ")
|
|
217
|
+
# Wire the coordinator's submit handler. Without this data-action the
|
|
218
|
+
# controller connects but onSubmit never fires, so an invalid form is not
|
|
219
|
+
# blocked client-side (issue #11). Joined with any caller-supplied action.
|
|
220
|
+
existing_action = attrs[:data][:action].to_s
|
|
221
|
+
submit_action = "submit->#{coordinator}#onSubmit"
|
|
222
|
+
attrs[:data][:action] = [existing_action, submit_action].reject(&:empty?).join(" ")
|
|
199
223
|
attrs[:novalidate] = true
|
|
200
224
|
end
|
|
201
225
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Forms
|
|
4
|
+
module Plain
|
|
5
|
+
# Bare checkbox group. Inherits the whole binding contract from
|
|
6
|
+
# Forms::CheckboxGroup (the shared array name, the empty-array hidden field,
|
|
7
|
+
# the per-item checked state) and overrides only the rendering seams to ship
|
|
8
|
+
# zero daisyUI classes. The invalid state rides aria-invalid on the group,
|
|
9
|
+
# never a color class.
|
|
10
|
+
class CheckboxGroup < Forms::CheckboxGroup
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# Bare <input type=checkbox>, no DaisyUI delegation, no styling classes.
|
|
14
|
+
def render_checkbox(option)
|
|
15
|
+
input(
|
|
16
|
+
type: "checkbox", name: @name, id: option[:id],
|
|
17
|
+
value: option[:value], class: @attributes[:class],
|
|
18
|
+
checked: option[:checked] || nil
|
|
19
|
+
)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def group_classes = @attributes[:class]
|
|
23
|
+
def item_classes = nil
|
|
24
|
+
def item_label_classes = nil
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -13,7 +13,11 @@ module Forms
|
|
|
13
13
|
# validation remains authoritative — the client side just
|
|
14
14
|
# shortens the loop for the common cases.
|
|
15
15
|
class Introspector
|
|
16
|
-
|
|
16
|
+
# The Stimulus identifier prefix. Kept in sync with the file path the
|
|
17
|
+
# controllers ship at (phlex_forms/controllers/validations/*_controller),
|
|
18
|
+
# so lazyLoadControllersFrom("phlex_forms/controllers") resolves
|
|
19
|
+
# `validations--length` → .../validations/length_controller (issue #12).
|
|
20
|
+
CONTROLLER_PREFIX = "validations"
|
|
17
21
|
|
|
18
22
|
# Validators we know how to mirror. Keys are the short class
|
|
19
23
|
# name (without namespace), values are the controller suffix
|
|
@@ -59,9 +63,9 @@ module Forms
|
|
|
59
63
|
|
|
60
64
|
# Returns a hash of the shape:
|
|
61
65
|
# {
|
|
62
|
-
# controller: "
|
|
63
|
-
#
|
|
64
|
-
#
|
|
66
|
+
# controller: "validations--presence validations--length",
|
|
67
|
+
# validations__presence_required_value: "true",
|
|
68
|
+
# validations__length_maximum_value: "60",
|
|
65
69
|
# }
|
|
66
70
|
#
|
|
67
71
|
# Returns {} when no supported validators are present.
|
|
@@ -95,7 +99,7 @@ module Forms
|
|
|
95
99
|
|
|
96
100
|
# Phlex turns underscores in `data:` hash keys into hyphens
|
|
97
101
|
# in the rendered HTML. To produce a key like
|
|
98
|
-
# `data-
|
|
102
|
+
# `data-validations--length-maximum-value` from a
|
|
99
103
|
# Ruby symbol we need every "-" represented as "__" in the
|
|
100
104
|
# symbol. That's what this method builds.
|
|
101
105
|
def data_key(suffix, key)
|
data/lib/phlex_forms/builder.rb
CHANGED
|
@@ -165,6 +165,9 @@ module PhlexForms
|
|
|
165
165
|
when :textarea then render fo.textarea(*modifiers, required:, **)
|
|
166
166
|
when :toggle then render fo.toggle(*modifiers, required:, **)
|
|
167
167
|
when :checkbox then render fo.checkbox(*modifiers, required:, **)
|
|
168
|
+
# required: doesn't apply to a group of checkboxes sharing one array name;
|
|
169
|
+
# validate the selection server-side instead.
|
|
170
|
+
when :checkbox_group then render_checkbox_group(fo, **)
|
|
168
171
|
when :file then render fo.file(*modifiers, required:, **)
|
|
169
172
|
when :hidden then render fo.hidden(**)
|
|
170
173
|
when :rich_textarea then render fo.rich_textarea(*modifiers, **)
|
|
@@ -177,6 +180,14 @@ module PhlexForms
|
|
|
177
180
|
end
|
|
178
181
|
end
|
|
179
182
|
|
|
183
|
+
# `f.field :tag_ids, as: :checkbox_group, collection: Tag.all, value:, label:`.
|
|
184
|
+
# collection: names the enumerable; the rest (value:/label:/variant:/size:)
|
|
185
|
+
# passes through to Field#checkbox_group. (choices:/required: are consumed by
|
|
186
|
+
# render_field_input's own signature, so they never reach here.)
|
|
187
|
+
def render_checkbox_group(fo, collection: [], **)
|
|
188
|
+
render fo.checkbox_group(collection, **)
|
|
189
|
+
end
|
|
190
|
+
|
|
180
191
|
def materialize_choices(choices)
|
|
181
192
|
choices.respond_to?(:call) ? choices.call : choices
|
|
182
193
|
end
|
data/lib/phlex_forms/theme.rb
CHANGED
|
@@ -55,6 +55,7 @@ module PhlexForms
|
|
|
55
55
|
input: Forms::Input, select: Forms::Select, choices_select: Forms::ChoicesSelect,
|
|
56
56
|
textarea: Forms::Textarea, rich_textarea: Forms::RichTextarea,
|
|
57
57
|
checkbox: Forms::Checkbox, toggle: Forms::Toggle, radio: Forms::Radio,
|
|
58
|
+
checkbox_group: Forms::CheckboxGroup,
|
|
58
59
|
file: Forms::FileInput, wrapped_input: Forms::WrappedInput,
|
|
59
60
|
control: Forms::FormControl, label: Forms::Label,
|
|
60
61
|
field_error: Forms::FieldError, field_hint: Forms::FieldHint,
|
|
@@ -72,6 +73,7 @@ module PhlexForms
|
|
|
72
73
|
input: Forms::Plain::Input, select: Forms::Plain::Select, choices_select: Forms::Plain::Select,
|
|
73
74
|
textarea: Forms::Plain::Textarea, rich_textarea: Forms::Plain::Textarea,
|
|
74
75
|
checkbox: Forms::Plain::Checkbox, toggle: Forms::Plain::Checkbox, radio: Forms::Plain::Radio,
|
|
76
|
+
checkbox_group: Forms::Plain::CheckboxGroup,
|
|
75
77
|
file: Forms::Plain::FileInput, wrapped_input: Forms::Plain::WrappedInput,
|
|
76
78
|
control: Forms::Plain::Control, label: Forms::Plain::Label,
|
|
77
79
|
field_error: Forms::Plain::FieldError, field_hint: Forms::Plain::FieldHint,
|
data/lib/phlex_forms/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: phlex-forms
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mikael Henriksson
|
|
@@ -114,6 +114,7 @@ files:
|
|
|
114
114
|
- config/rubocop.yml
|
|
115
115
|
- lib/forms/base.rb
|
|
116
116
|
- lib/forms/checkbox.rb
|
|
117
|
+
- lib/forms/checkbox_group.rb
|
|
117
118
|
- lib/forms/choices_select.rb
|
|
118
119
|
- lib/forms/collection_check_box.rb
|
|
119
120
|
- lib/forms/collection_check_box_builder.rb
|
|
@@ -133,6 +134,7 @@ files:
|
|
|
133
134
|
- lib/forms/live/field.rb
|
|
134
135
|
- lib/forms/password_field.rb
|
|
135
136
|
- lib/forms/plain/checkbox.rb
|
|
137
|
+
- lib/forms/plain/checkbox_group.rb
|
|
136
138
|
- lib/forms/plain/control.rb
|
|
137
139
|
- lib/forms/plain/field_error.rb
|
|
138
140
|
- lib/forms/plain/field_hint.rb
|