Add full_error.

This commit is contained in:
José Valim 2011-04-27 21:58:59 +02:00
parent 39bc7798b7
commit 726040adf9
4 changed files with 46 additions and 1 deletions

View File

@ -11,6 +11,7 @@
* Add option to disable all HTML5 extensions (by github.com/wolframarnold)
* Add input_field helper to form builder (by github.com/jeroenhouben)
* Allow inputs to be discovered on demand by placing them at app/inputs (a la formtastic)
* Add full_error on that shows the error with the attribute name
* bug fix
* Fix for file attributes automatic detection, to work with virtual attributes

View File

@ -12,7 +12,11 @@ module SimpleForm
end
def error_text
errors.send(error_method)
if options[:error_prefix]
options[:error_prefix] + " " + errors.send(error_method)
else
errors.send(error_method)
end
end
def error_method

View File

@ -204,6 +204,24 @@ module SimpleForm
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).error
end
# Return the error but also considering its name. This is used
# when errors for a hidden field need to be shown.
#
# == Examples
#
# f.full_error :token #=> <span class="error">Token is invalid</span>
#
def full_error(attribute_name, options={})
prefix = options.delete(:prefix) || if object.class.respond_to?(:human_attribute_name)
object.class.human_attribute_name(attribute_name.to_s)
else
attribute_name.to_s.humanize
end
options[:error_prefix] = prefix
error(attribute_name, options)
end
# Creates a hint tag for the given attribute. Accepts a symbol indicating
# an attribute for I18n lookup or a string. All the given options are sent
# as :hint_html.

View File

@ -27,6 +27,12 @@ class FormBuilderTest < ActionView::TestCase
end
end
def with_full_error_for(object, *args)
with_concat_form_for(object) do |f|
f.full_error(*args)
end
end
def with_hint_for(object, *args)
with_concat_form_for(object) do |f|
f.hint(*args)
@ -455,6 +461,22 @@ class FormBuilderTest < ActionView::TestCase
assert_select 'span.error#name_error', "can't be blank"
end
# FULL ERRORS
test 'builder should generate an full error tag for the attribute' do
with_full_error_for @user, :name
assert_select 'span.error', "Super User Name! can't be blank"
end
test 'builder should generate an full error tag with a clean HTML' do
with_full_error_for @user, :name
assert_no_select 'span.error[error_html]'
end
test 'builder should allow passing options to full error tag' do
with_full_error_for @user, :name, :id => 'name_error', :prefix => "Your name"
assert_select 'span.error#name_error', "Your name can't be blank"
end
# HINTS
test 'builder should generate a hint tag for the attribute' do
store_translations(:en, :simple_form => { :hints => { :user => { :name => "Add your name" }}}) do