constraint-validation-example

Expand History Expand History
Collapse History Collapse History

Validation message from server

For each field, encode an ActiveModel::Error-generated validation message that corresponds to a ValidityState key into the field’s [data-validation-messages] attribute. When handling invalid events, retrieve the corresponding error message from that Object and render it when available.

When a key in a field’s ValidityState is absent from the data-validation-messages, fallback to the Browser-provided default.

To force this situation, declare an arbitrary length: { minimum: 7 } validation on the Authentication#password attribute.

Mapping ActiveModel::Errors to Validation messages

The application/validation_messages JSON partial is an incomplete mapping. There are some attribute and validation-specific subtleties whose solutions are not yet aren’t immediately obvious. For example, length validations encode a %{count} interpolation placeholder. Some validation concepts are flat-out missing counterparts.

There is a possibility to implement a lightweight I18n gem counterpart on the client side to replace interpolations with the correct values at validation-time, but at this point in time, that is an exercise best left up to the reader.