mirror of
https://github.com/heartcombo/simple_form.git
synced 2022-11-09 12:19:26 -05:00
Merge pull request #975 from mantas/master
Allows custom errors #761 Conflicts: lib/simple_form/components/errors.rb test/form_builder/error_test.rb
This commit is contained in:
commit
402623c200
3 changed files with 20 additions and 3 deletions
|
@ -10,7 +10,7 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def has_errors?
|
||||
object && object.respond_to?(:errors) && errors.present?
|
||||
(object && object.respond_to?(:errors) && errors.present?) || has_error_in_options?
|
||||
end
|
||||
|
||||
protected
|
||||
|
@ -36,7 +36,7 @@ module SimpleForm
|
|||
end
|
||||
|
||||
def errors_on_attribute
|
||||
object.errors[attribute_name]
|
||||
has_error_in_options? ? [options[:error]] : object.errors[attribute_name]
|
||||
end
|
||||
|
||||
def full_errors_on_attribute
|
||||
|
@ -50,6 +50,10 @@ module SimpleForm
|
|||
def full_errors_on_association
|
||||
reflection ? object.full_messages_for(reflection.name) : []
|
||||
end
|
||||
|
||||
def has_error_in_options?
|
||||
options[:error] && !options[:error].nil?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -45,7 +45,7 @@ module SimpleForm
|
|||
end
|
||||
|
||||
# Always enabled.
|
||||
enable :hint
|
||||
enable :hint, :error
|
||||
|
||||
# Usually disabled, needs to be enabled explicitly passing true as option.
|
||||
disable :maxlength, :placeholder, :pattern, :min_max
|
||||
|
|
|
@ -14,6 +14,10 @@ class ErrorTest < ActionView::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def with_custom_error_for(object, *args)
|
||||
with_form_for(object, *args)
|
||||
end
|
||||
|
||||
test 'error should not generate content for attribute without errors' do
|
||||
with_error_for @user, :active
|
||||
assert_no_select 'span.error'
|
||||
|
@ -132,4 +136,13 @@ class ErrorTest < ActionView::TestCase
|
|||
assert_select 'span.error', 'Company must be valid'
|
||||
end
|
||||
end
|
||||
|
||||
# CUSTOM ERRORS
|
||||
|
||||
test 'input with custom error works' do
|
||||
error_text = "Super User Name! can't be blank"
|
||||
with_custom_error_for(@user, :name, error: error_text)
|
||||
|
||||
assert_select 'span.error', "#{error_text}"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue