Adds a helper for displaying validation error messages.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@9017 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-02-26 18:07:13 +00:00
parent 9c264a7e66
commit 7c568cccaf
6 changed files with 20 additions and 5 deletions

View File

@ -945,6 +945,20 @@ module ApplicationHelper
remote_form_for(*args, &proc) remote_form_for(*args, &proc)
end end
def error_messages_for(*objects)
html = ""
objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact
errors = objects.map {|o| o.errors.full_messages}.flatten
if errors.any?
html << "<div id='errorExplanation'><ul>\n"
errors.each do |error|
html << "<li>#{h error}</li>\n"
end
html << "</ul></div>\n"
end
html.html_safe
end
def back_url_hidden_field_tag def back_url_hidden_field_tag
back_url = params[:back_url] || request.env['HTTP_REFERER'] back_url = params[:back_url] || request.env['HTTP_REFERER']
back_url = CGI.unescape(back_url.to_s) back_url = CGI.unescape(back_url.to_s)

View File

@ -1,4 +1,4 @@
<%= f.error_messages %> <%= error_messages_for @board %>
<div class="box tabular"> <div class="box tabular">
<p><%= f.text_field :name, :required => true %></p> <p><%= f.text_field :name, :required => true %></p>

View File

@ -1,4 +1,4 @@
<%= f.error_messages %> <%= error_messages_for @document %>
<div class="box tabular"> <div class="box tabular">
<p><%= f.select :category_id, DocumentCategory.active.collect {|c| [c.name, c.id]} %></p> <p><%= f.select :category_id, DocumentCategory.active.collect {|c| [c.name, c.id]} %></p>

View File

@ -1,4 +1,4 @@
<%= f.error_messages %> <%= error_messages_for @group %>
<div class="box tabular"> <div class="box tabular">
<p><%= f.text_field :lastname, :label => :field_name %></p> <p><%= f.text_field :lastname, :label => :field_name %></p>

View File

@ -1,4 +1,5 @@
<%= f.error_messages %> <%= error_messages_for @news %>
<div class="box tabular"> <div class="box tabular">
<p><%= f.text_field :title, :required => true, :size => 60 %></p> <p><%= f.text_field :title, :required => true, :size => 60 %></p>
<p><%= f.text_area :summary, :cols => 60, :rows => 2 %></p> <p><%= f.text_area :summary, :cols => 60, :rows => 2 %></p>

View File

@ -95,7 +95,7 @@ class MyControllerTest < ActionController::TestCase
:new_password_confirmation => 'hello2' :new_password_confirmation => 'hello2'
assert_response :success assert_response :success
assert_template 'password' assert_template 'password'
assert_tag :tag => "div", :attributes => { :class => "errorExplanation" } assert_error_tag :content => /Password doesn't match confirmation/
# wrong password # wrong password
post :password, :password => 'wrongpassword', post :password, :password => 'wrongpassword',