Validation

Client-side validation

validate: true mirrors your ActiveModel validators into shipped Stimulus controllers — the no-phlex-reactive fallback.

Turning it on#

validate: true introspects the model's validators and emits data-validations--* bindings per field, plus a form-level submit coordinator. The form gets novalidate — the shipped Stimulus controllers own error display, so you never see the inconsistent native browser bubbles.

Form(model: @partner, validate: true) do |f|
  f.field :title
  f.field :note
  f.submit :primary
end

Register the controllers once (see Installation):

app/javascript/controllers/index.js
import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading"
lazyLoadControllersFrom("phlex_forms/controllers", application)

Per-field overrides#

Form(model: @partner, validate: true) do |f|
  f.field :title                                      # every validator on :title
  f.field :slug, validate: false                      # opt this field out
  f.field :note, validate: { length: { maximum: 30 } } # explicit inline rules
end

What's supported#

Presence, length (with a live character counter), format, numericality, inclusion, exclusion, confirmation, and acceptance.

Validators with :if, :unless, or :on are skipped — they need server context — and uniqueness can't be checked client-side. The server stays authoritative either way; this layer is UX, not enforcement.

If those gaps matter, live validation closes all of them by running the real validators server-side.

Length counter#

A live n / limit next to the field, with an optional recommended length.

A length rule with a maximum: renders a live n / maximum counter. Add recommended: to count against a lower, advisory length: it never adds an error, never becomes maxlength, and the server ignores it (ActiveModel keeps the unknown option without reading it).

f.field :title, as: :textarea, validate: { length: { maximum: 50, recommended: 32 } }

# or on the model; the introspector reads it the same way
validates :title, length: { maximum: 50, recommended: 32 }
LengthCounterClass
up to recommendedn / recommendednone
past recommended, up to maximumn / maximumtext-warning
past maximumn / maximumtext-error

Past the maximum the field also shows the too_long message on blur and blocks the submit, as before. Without a maximum:, the counter reads n / recommended and turns text-warning past it. An inline validate: { length: { recommended: 32 } } needs no bound; a model-level validates … length: still needs one, because ActiveModel requires it there. A recommended: that is not a positive Integer below the maximum, or one next to an exact is:, is ignored, and the counter behaves as if it were not set.

The counter's text-warning and text-error classes are toggled from the gem's JavaScript, so your Tailwind build must include them. The counter comes from these client-side rules only: live validation's server round trip adds none, so a live field shows one only when it also carries client-side length rules.

Messages & i18n#

A validator's own message: reaches the browser, so the client shows the text the server would add:

  • a String is used as is; %{value} is filled in client-side, and %{count} for rules that have one (length, the numericality bounds).
  • a Symbol is resolved on the server through ActiveModel's lookup chain (activemodel.errors.models.<model>.attributes.<attr>.<key>, then errors.messages.<key>) in the current locale.
  • a Proc is skipped (it needs a record); the generic message shows.

Length's too_long:, too_short: and wrong_length: win over message:, as on the server. An inline validate: { ... } hash takes the same options; a Symbol there has no model behind it, so it resolves through errors.messages.<key> only, with %{attribute} reading "Value".

validates :code, format: { with: /\A[a-z]*\z/, message: "may only contain lowercase letters" }
validates :bio, length: { maximum: 160, too_long: "keep it under %{count} characters" }

Without a message:, the bundled messages apply. They ship for en, fr, and af; override any string page-wide via window.PhlexForms.messages. The engine prepends the gem's locale files to I18n.load_path, so your app's files win.