ruby-on-railsruby-on-rails-6form-forform-with

Change 'Please match the format requested.' to something else?


Is there an easy way to customise the message that appears when the pattern: "([A-Za-z0-9\-\_]+)" argument is not satisfied and this message appears:

> Please match the format requested

e.g.

<%= f.text_field :username, pattern: "([A-Za-z0-9\-\_]+)" %>

Solution

  • You can add a "title" attribute to append on to that message:

    <%= f.text_field :username, pattern: "([A-Za-z0-9\-\_]+)" , title: "Letters, numbers, hyphen, and underscore Only"%>
    

    This will result in the following message:

    Please match the format requested: Letters, numbers, hyphen, and underscore Only

    Otherwise, for more custom messages, you can use client side validation (via javascript) server side validations (via rails and ActiveModel)