Add the `full_error` component.
With this wrapper, it will call full_messages_for the attribute.
This commit is contained in:
parent
838eddeb4c
commit
4e36df53ad
|
@ -1,6 +1,7 @@
|
||||||
## master
|
## master
|
||||||
|
|
||||||
### enhancements
|
### enhancements
|
||||||
|
* Add the `full_error` component. [@laurocaetano](https://github.com/laurocaetano)
|
||||||
* Add support to `scope` to be used on associations. [@laurocaetano](https://github.com/laurocaetano)
|
* Add support to `scope` to be used on associations. [@laurocaetano](https://github.com/laurocaetano)
|
||||||
* Execute the association `condition` in the object context. [@laurocaetano](https://github.com/laurocaetano)
|
* Execute the association `condition` in the object context. [@laurocaetano](https://github.com/laurocaetano)
|
||||||
* Check if the given association responds to `order` before calling it. [@laurocaetano](https://github.com/laurocaetano)
|
* Check if the given association responds to `order` before calling it. [@laurocaetano](https://github.com/laurocaetano)
|
||||||
|
|
|
@ -43,6 +43,12 @@ SimpleForm.setup do |config|
|
||||||
b.use :label_input
|
b.use :label_input
|
||||||
b.use :hint, wrap_with: { tag: :span, class: :hint }
|
b.use :hint, wrap_with: { tag: :span, class: :hint }
|
||||||
b.use :error, wrap_with: { tag: :span, class: :error }
|
b.use :error, wrap_with: { tag: :span, class: :error }
|
||||||
|
|
||||||
|
## full_messages_for
|
||||||
|
# If you want to display the full error message for the attribute, you can
|
||||||
|
# use the component :full_error, like:
|
||||||
|
#
|
||||||
|
# b.use :full_error, wrap_with: { tag: :span, class: :error }
|
||||||
end
|
end
|
||||||
|
|
||||||
# The default wrapper to be used by the FormBuilder.
|
# The default wrapper to be used by the FormBuilder.
|
||||||
|
|
|
@ -5,6 +5,10 @@ module SimpleForm
|
||||||
error_text if has_errors?
|
error_text if has_errors?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def full_error(wrapper_options = nil)
|
||||||
|
full_error_text if has_errors?
|
||||||
|
end
|
||||||
|
|
||||||
def has_errors?
|
def has_errors?
|
||||||
object && object.respond_to?(:errors) && errors.present?
|
object && object.respond_to?(:errors) && errors.present?
|
||||||
end
|
end
|
||||||
|
@ -15,6 +19,10 @@ module SimpleForm
|
||||||
"#{html_escape(options[:error_prefix])} #{errors.send(error_method)}".lstrip.html_safe
|
"#{html_escape(options[:error_prefix])} #{errors.send(error_method)}".lstrip.html_safe
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def full_error_text
|
||||||
|
"#{html_escape(options[:error_prefix])} #{full_errors.send(error_method)}".lstrip.html_safe
|
||||||
|
end
|
||||||
|
|
||||||
def error_method
|
def error_method
|
||||||
options[:error_method] || SimpleForm.error_method
|
options[:error_method] || SimpleForm.error_method
|
||||||
end
|
end
|
||||||
|
@ -23,13 +31,25 @@ module SimpleForm
|
||||||
@errors ||= (errors_on_attribute + errors_on_association).compact
|
@errors ||= (errors_on_attribute + errors_on_association).compact
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def full_errors
|
||||||
|
@full_errors ||= (full_errors_on_attribute + full_errors_on_association).compact
|
||||||
|
end
|
||||||
|
|
||||||
def errors_on_attribute
|
def errors_on_attribute
|
||||||
object.errors[attribute_name]
|
object.errors[attribute_name]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def full_errors_on_attribute
|
||||||
|
object.errors.full_messages_for(attribute_name)
|
||||||
|
end
|
||||||
|
|
||||||
def errors_on_association
|
def errors_on_association
|
||||||
reflection ? object.errors[reflection.name] : []
|
reflection ? object.errors[reflection.name] : []
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def full_errors_on_association
|
||||||
|
reflection ? object.full_messages_for(reflection.name) : []
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -123,4 +123,13 @@ class ErrorTest < ActionView::TestCase
|
||||||
assert_select 'span.omg_error', "can't be blank"
|
assert_select 'span.omg_error', "can't be blank"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# FULL_ERROR_WRAPPER
|
||||||
|
|
||||||
|
test 'full error should find errors on association' do
|
||||||
|
swap_wrapper :default, self.custom_wrapper_with_full_error do
|
||||||
|
with_form_for @user, :company_id, as: :select
|
||||||
|
assert_select 'span.error', 'Company must be valid'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -136,6 +136,13 @@ class WrapperTest < ActionView::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test 'custom wrappers can have full error message on attributes' do
|
||||||
|
swap_wrapper :default, self.custom_wrapper_with_full_error do
|
||||||
|
with_form_for @user, :name
|
||||||
|
assert_select 'span.error', "Name can't be blank"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test 'custom wrappers on a form basis' do
|
test 'custom wrappers on a form basis' do
|
||||||
swap_wrapper :another do
|
swap_wrapper :another do
|
||||||
with_concat_form_for(@user) do |f|
|
with_concat_form_for(@user) do |f|
|
||||||
|
|
|
@ -144,6 +144,12 @@ module MiscHelpers
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def custom_wrapper_with_full_error
|
||||||
|
SimpleForm.build tag: :div, class: 'custom_wrapper' do |b|
|
||||||
|
b.use :full_error, wrap_with: { tag: :span, class: :error }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def custom_wrapper_with_label_text
|
def custom_wrapper_with_label_text
|
||||||
SimpleForm.build :label_text => proc { |label, required| "**#{label}**" } do |b|
|
SimpleForm.build :label_text => proc { |label, required| "**#{label}**" } do |b|
|
||||||
b.use :label_input
|
b.use :label_input
|
||||||
|
|
Loading…
Reference in New Issue