Skip to content

feat(builder): hidden_field alias for Rails FormBuilder migration - #21

Merged
mhenrixon merged 1 commit into
mainfrom
feature/hidden-field-builder-method
Jul 11, 2026
Merged

feat(builder): hidden_field alias for Rails FormBuilder migration#21
mhenrixon merged 1 commit into
mainfrom
feature/hidden-field-builder-method

Conversation

@mhenrixon

Copy link
Copy Markdown
Collaborator

Summary

form.hidden_field(:accepted_terms_document_id, value: TermsDocument.current&.id) raised:

NoMethodError:
  Try including `Phlex::Rails::Helpers::HiddenField` in Forms::Form, like so:
  ...

Forms::Form exposes the PascalCase escape hatch f.Hidden(:name) but no snake_case Rails FormBuilder API, so straight hidden_field migrations from ActionView hit a NoMethodError.

This adds hidden_field to PhlexForms::Builder as a snake_case alias that delegates to the same model-bound Field#hidden path as Hidden. An explicit value: still wins over the model's current value (kwargs merge order in Field#hidden), so f.hidden_field(:token, value: x) ports over verbatim. Because it lives on the shared Builder mixin, both Forms::Form and FieldsForBuilder (nested fields_for) get it.

Test Coverage

  • hidden_field renders a scoped hidden input honoring an explicit value: override (name="user[accepted_terms_document_id]", value="42")
  • binds the value from the model when none is passed (value="abc123")
  • renders under the plain theme too (bare <input type="hidden">) — theme parity

Verification

  • bundle exec rubocop lib spec — clean
  • bundle exec rspec — 165 passed, 1 pending

Deviations & judgment calls

  • Reused the existing path, no new value logic. Field#hidden already reads value: field_value from the model and lets a caller value: in the splat win. hidden_field delegates to it unchanged — no duplicate value-handling.
  • Left the LegacyFormMethod cop unchanged. It still nudges hidden_fieldHidden. The method now runs (enables migration) while the cop keeps steering hosts toward the idiomatic API over time — a lint nudge and a working method are complementary, not contradictory.
  • Scoped to hidden_field only — the one reported failure — not the whole Rails *_field family. The cop's INPUT_TYPE_METHODS / OTHER_LEGACY_METHODS maps enumerate the rest; if hosts hit more NoMethodErrors mid-migration, those are the follow-up list.
  • Added to the Builder mixin, not just Forms::Form, so nested fields_for builders get it for free — both include the mixin.

## Summary
`form.hidden_field(:accepted_terms_document_id, value: x)` raised
NoMethodError because Forms::Form has no snake_case Rails FormBuilder
API — only the PascalCase `Hidden` escape hatch. Add `hidden_field` to
PhlexForms::Builder as a snake_case alias delegating to the same
model-bound `Field#hidden` path, so Rails hosts can migrate hidden-field
call sites verbatim. An explicit `value:` still wins over the model's
current value.

Lives on the Builder mixin, so both Forms::Form and FieldsForBuilder
(nested fields_for) get it.

## Test Coverage
- renders a scoped hidden input honoring an explicit value: override
- binds value from the model when none is passed
- renders under the plain theme too (bare hidden input)

## Verification
- [x] bundle exec rubocop lib spec passes
- [x] bundle exec rspec passes (165 passed, 1 pending)
@mhenrixon mhenrixon self-assigned this Jul 11, 2026
@mhenrixon mhenrixon added the bug Something isn't working label Jul 11, 2026
@mhenrixon
mhenrixon merged commit 0a22a24 into main Jul 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant