This repository has been archived by the owner on May 8, 2022. It is now read-only.
forked from rails/dynamic_form
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module ActiveModel | ||
class Errors | ||
# Redefine the ActiveModel::Errors::full_messages method: | ||
# Returns all the full error messages in an array. 'Base' messages are handled as usual. | ||
# Non-base messages are prefixed with the attribute name as usual UNLESS | ||
# (1) they begin with '^' in which case the attribute name is omitted. | ||
# E.g. validates_acceptance_of :accepted_terms, :message => '^Please accept the terms of service' | ||
# (2) the message is a proc, in which case the proc is invoked on the model object. | ||
# E.g. validates_presence_of :assessment_answer_option_id, | ||
# :message => Proc.new { |aa| "#{aa.label} (#{aa.group_label}) is required" } | ||
# which gives an error message like: | ||
# Rate (Accuracy) is required | ||
def full_messages | ||
full_messages = [] | ||
|
||
each do |attribute, messages| | ||
messages = Array.wrap(messages) | ||
next if messages.empty? | ||
|
||
if attribute == :base | ||
messages.each {|m| full_messages << m } | ||
else | ||
attr_name = attribute.to_s.gsub('.', '_').humanize | ||
attr_name = @base.class.human_attribute_name(attribute, :default => attr_name) | ||
options = { :default => "%{attribute} %{message}", :attribute => attr_name } | ||
|
||
messages.each do |m| | ||
if m =~ /^\^/ | ||
options[:default] = "%{message}" | ||
full_messages << I18n.t(:"errors.dynamic_format", options.merge(:message => m[1..-1])) | ||
elsif m.is_a? Proc | ||
options[:default] = "%{message}" | ||
full_messages << I18n.t(:"errors.dynamic_format", options.merge(:message => m.call(@base))) | ||
else | ||
full_messages << I18n.t(:"errors.format", options.merge(:message => m)) | ||
end | ||
end | ||
end | ||
end | ||
|
||
full_messages | ||
end | ||
end | ||
end | ||
|
||
require 'active_support/i18n' | ||
I18n.load_path << File.dirname(__FILE__) + '/locale/en.yml' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
en: | ||
errors: | ||
# The default format to use in full dynamic error messages. | ||
dynamic_format: "%{message}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
require "active_model/dynamic_errors" | ||
require 'action_view/helpers/dynamic_form' | ||
|
||
class ActionView::Base | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters