Add full_error.
This commit is contained in:
parent
39bc7798b7
commit
726040adf9
|
@ -11,6 +11,7 @@
|
||||||
* Add option to disable all HTML5 extensions (by github.com/wolframarnold)
|
* Add option to disable all HTML5 extensions (by github.com/wolframarnold)
|
||||||
* Add input_field helper to form builder (by github.com/jeroenhouben)
|
* 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)
|
* 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
|
* bug fix
|
||||||
* Fix for file attributes automatic detection, to work with virtual attributes
|
* Fix for file attributes automatic detection, to work with virtual attributes
|
||||||
|
|
|
@ -12,8 +12,12 @@ module SimpleForm
|
||||||
end
|
end
|
||||||
|
|
||||||
def error_text
|
def error_text
|
||||||
|
if options[:error_prefix]
|
||||||
|
options[:error_prefix] + " " + errors.send(error_method)
|
||||||
|
else
|
||||||
errors.send(error_method)
|
errors.send(error_method)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def error_method
|
def error_method
|
||||||
options[:error_method] || SimpleForm.error_method
|
options[:error_method] || SimpleForm.error_method
|
||||||
|
|
|
@ -204,6 +204,24 @@ module SimpleForm
|
||||||
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).error
|
SimpleForm::Inputs::Base.new(self, attribute_name, column, input_type, options).error
|
||||||
end
|
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
|
# 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
|
# an attribute for I18n lookup or a string. All the given options are sent
|
||||||
# as :hint_html.
|
# as :hint_html.
|
||||||
|
|
|
@ -27,6 +27,12 @@ class FormBuilderTest < ActionView::TestCase
|
||||||
end
|
end
|
||||||
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)
|
def with_hint_for(object, *args)
|
||||||
with_concat_form_for(object) do |f|
|
with_concat_form_for(object) do |f|
|
||||||
f.hint(*args)
|
f.hint(*args)
|
||||||
|
@ -455,6 +461,22 @@ class FormBuilderTest < ActionView::TestCase
|
||||||
assert_select 'span.error#name_error', "can't be blank"
|
assert_select 'span.error#name_error', "can't be blank"
|
||||||
end
|
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
|
# HINTS
|
||||||
test 'builder should generate a hint tag for the attribute' do
|
test 'builder should generate a hint tag for the attribute' do
|
||||||
store_translations(:en, :simple_form => { :hints => { :user => { :name => "Add your name" }}}) do
|
store_translations(:en, :simple_form => { :hints => { :user => { :name => "Add your name" }}}) do
|
||||||
|
|
Loading…
Reference in New Issue