constraint-validation-example

Expand History Expand History
Collapse History Collapse History

ActionView::Helpers::FormBuilder extensions

Start to abstract validation message rendering by declare a descendant to the ActionView::Helpers::FormBuilder class named ValidationMessageFormBuilder. Set it as the application’s default by calling default_form_builder from the ApplicationController class.

First and foremost, declare the #errors method, which retrieves validation messages for an attribute. When passed a block, yield the collection of ActiveModel::Errors. When no block is passed, return the collection of ActiveModel::Errors.

Next, ensure that calls to form field builders set [aria-invalid="true"] when rendering a field with error associated to the attribute. When rendering checked-capable fields like <input type="checkbox"> and <input type="radio">, only render [aria-invalid="true"] on the element that is also checked.