TextField

Text input fields with support for various HTML5 types, sizes, labels, hints, and validation.

Usage

<%= form_with url: root_path do |f| %>
  <%= f.text_field :name %>
  <%= f.email_field :email %>
  <%= f.password_field :password %>
  <%= f.phone_field :phone %>
<% end %>
Props
PropTypeDefaultDescription
sizeSymbol
:sm | :md | :lg
:mdSize of the text field.
labelString | falsehumanized attribute nameCustom label text. Set to `false` to hide the label.
hintStringnilHelper text displayed below the field.
errorStringnilManual error message. ActiveRecord errors are displayed automatically.
requiredBooleanfalseMark the field as required.
disabledBooleanfalseDisable the field.

Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.

Examples

TextField types

<%= form_with url: root_path do |f| %>
  <%= f.text_field :first_name, required: true %>
  <%= f.email_field :email, autocomplete: "email" %>
  <%= f.password_field :password, autocomplete: "current-password" %>
  <%= f.phone_field :phone, placeholder: "+1 (555) 123-4567" %>
<% end %>

TextField sizes

<%= form_with url: root_path do |f| %>
  <%= f.text_field :large_field, size: :lg %>
  <%= f.text_field :default_field %>
  <%= f.text_field :small_field, size: :sm %>
<% end %>

Label customization

<%= form_with url: root_path do |f| %>
  <%= f.text_field :custom_label, label: "Custom Label Text" %>
  <%= f.text_field :no_label, label: false, placeholder: "Field without label" %>
  <%= f.text_field :default_label %>
<% end %>

Hint messages

Must be at least 8 characters long

We'll never share your email with anyone else

<%= form_with url: root_path do |f| %>
  <%= f.text_field :password, label: "Password", hint: "Must be at least 8 characters long" %>
  <%= f.email_field :email, hint: "We'll never share your email with anyone else" %>
<% end %>

Disabled State

<%= form_with url: root_path do |f| %>
  <%= f.text_field :disabled_field, value: "Disabled", disabled: true %>
<% end %>

Validation

Is required

This field has a custom error

<% user = User.new %>
<% user.errors.add(:first_name, "is required") %>
<%= form_with model: user, url: root_path do |f| %>
  <%= f.text_field :first_name, label: "Active Record Validations" %>
  <%= f.text_field :manual_error_field, error: "This field has a custom error", value: "Invalid Value" %>
<% end %>