Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customizing Model Validation error messages alerts

I'm trying to customize the error message alert that users see at the top of a form when they input data incorrectly. The error message alert I'm trying to customize are for Model Attributes which are in a nested form.

I've tried the solution here which says to edit the config/locales/en.yml file but this only changes the message not the name of the model & attribute which are displayed before the error message.

I've also tried what Billy's suggested in his answer bellow which has the same result. i.e.

1 error prohibited this hikingtrail from being saved:
- Directions directions from 'My Custom Blank Error Message'

Is there a way for me to display a more user friendly Model & attribute name in my error message or remove them entirely from the error message?

Here is what I have:

config/locales/en.yml

    # Sample localization file for English. Add more files in this directory for other locales.
    # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
en:  
  activerecord:
    models: 
      direction: "In the Getting There section"
    attributes:
      direction:
        directions_from: "From field"
    errors:
      full_messages:
      format: "%{message}"
      models:
        direction:
          attributes:
            directions_from:
              blank: "My Custom Blank Error Message"

Model

class Direction < ActiveRecord::Base
  belongs_to :hikingtrail

  attr_accessible :directions_by, :directions_desc, :directions_from

  validates :directions_from, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_by? || a.directions_desc? } }

  validates :directions_by, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_from? || a.directions_desc? } }

  validates :directions_desc, :presence => {message: "'My Custom Error Message'", :if => Proc.new { |a| a.directions_from? || a.directions_by? } }
end
like image 813
Holly Avatar asked Mar 29 '13 01:03

Holly


People also ask

How do I display custom error messages?

To display a custom error page with an appropriate error code, use the <httpErrors> section only, and do not use the <customErrors> section. Add the following <httpErrors> section under <system. webServer> section, as shown below.

What is custom error message?

Drivers can specify their own error types and error messages. To define a custom error message, you must first define a new IO_ERR_XXX value to specify as the ErrorCode member of the error log entry. The Event Viewer uses the IO_ERR_XXX value to look up the driver's error message.

What is validation error message?

The Validation Error Message property lets you define a custom error message to display if the validation checks specified in the Validation (Regex) fails.

What is a validation message?

When you have validation enabled, you can receive messages that validate a filter or function expression. You can receive either warnings or errors based on the information that you have specified in your mapping specification.


1 Answers

You can use :message option to assign custom error message.

Example:

validates :directions_from, presence: true, 
  message: "'Direction from' really really really can't be blank!"

Then this custom error message will appear as <%= msg %> in the form view.

Ref: http://edgeguides.rubyonrails.org/active_record_validations.html#message

Add To answer OP's question on the comment, i.e. the message shown in web page is not very friendly, showing result as "Directions directions from 'Direction from' really really really can't be blank"

The reason is the view template use errors.full_messages to show the error messages. You can easily customize it with two options:

Option 1: Write the custom message without subject. i.e. really can't be blank

Option 2: Write the message as before in full sentence, but refer to message only in view, instead of full_message

Example:

<% @hikingtrail.errors.messages.each do |msg| %>
    <li><%= msg %></li>
<% end %>

Ref: http://rubydoc.info/docs/rails/3.2.8/ActiveModel/Errors (full_message is nothing more but a mix of attribute and message)

like image 118
Billy Chan Avatar answered Sep 30 '22 12:09

Billy Chan