Sha256: 0d29cf78bfcac8933cd870174e885ea517deb3a2494ba8c7688b668d2eaa1ecd

Contents?: true

Size: 1.06 KB

Versions: 140

Compression:

Stored size: 1.06 KB

Contents

---
title: "Validations"
id: validations
---

In Rails, forms usually have an associated model object. Models define validations which include validation messages that describe which validations failed and why. Primer forms automatically display validation messages when a model object is provided.

## Custom validation messages

All form inputs allow overriding the default validation message via the `:validation_message` argument:

<%= code :ruby do %>
  class ExampleForm < ApplicationForm
    form do |example_form|
      example_form.text_field(
        name: :foo,
        label: "Foo",
        validation_message: "Custom validation message"
      )
    end
  end
<% end %>

A custom validation message is also useful if no associated model object is available, i.e. for forms that are not tied to a database table.

## Input focus

Primer forms will automatically focus the first invalid input in accordance with accessibility best-practices. Inputs are considered invalid if the associated model's field is invalid _or_ if a custom validation message has been provided.

Version data entries

140 entries across 140 versions & 2 rubygems

Version Path
primer_view_components-0.19.0 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.22.2 previews/pages/forms/08_validations.md.erb
primer_view_components-0.18.2 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.22.1 previews/pages/forms/08_validations.md.erb
primer_view_components-0.18.1 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.22.0 previews/pages/forms/08_validations.md.erb
primer_view_components-0.18.0 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.21.0 previews/pages/forms/08_validations.md.erb
primer_view_components-0.17.0 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.20.0 previews/pages/forms/08_validations.md.erb
primer_view_components-0.16.1 previews/pages/forms/08_validations.md.erb
primer_view_components-0.16.0 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.19.0 previews/pages/forms/08_validations.md.erb
primer_view_components-0.15.1 previews/pages/forms/08_validations.md.erb
primer_view_components-0.15.0 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.18.1 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.17.1 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.17.0 previews/pages/forms/08_validations.md.erb
primer_view_components-0.14.0 previews/pages/forms/08_validations.md.erb
openproject-primer_view_components-0.16.0 previews/pages/forms/08_validations.md.erb