diff --git a/Gemfile b/Gemfile index 8d78e8e..175942b 100644 --- a/Gemfile +++ b/Gemfile @@ -2,4 +2,4 @@ source "https://rubygems.org" gemspec -gem 'dry-validation', '~> 1.5.0' +gem 'dry-validation' diff --git a/lib/reform/errors.rb b/lib/reform/errors.rb index c4e3817..6655bf8 100644 --- a/lib/reform/errors.rb +++ b/lib/reform/errors.rb @@ -26,11 +26,15 @@ def messages(*args) @dotted_errors end - def full_messages - @dotted_errors.collect { |path, errors| - human_field = path.to_s.gsub(/([\.\_])+/, " ").gsub(/(\b\w)+/) { |s| s.capitalize } - errors.collect { |message| "#{human_field} #{message}" } - }.flatten + def full_messages(errors = @dotted_errors) + errors.collect { |path, errors| + human_field = path.to_s.gsub(/([\.\_])+/, " ").gsub(/(\b\w)+/) { |s| s.capitalize } + errors.collect { |message| "#{human_field} #{message}" } + }.flatten + end + + def full_messages_for(field) + full_messages(@dotted_errors.select{ |path| path.to_s == field.to_s }) end def [](name) diff --git a/test/errors_test.rb b/test/errors_test.rb index f5ab67c..9fcde2d 100644 --- a/test/errors_test.rb +++ b/test/errors_test.rb @@ -174,6 +174,21 @@ class AlbumForm < TestForm end end + describe "#full_messages_for" do + it "renders full messages for given field correctly" do + result = form.validate( + "title" => "", + "artists" => [], + "band" => {"name" => "", "label" => {"name" => ""}} + ) + + assert_equal result, false + assert_equal form.errors.full_messages_for(:title), ["Title must be filled"] + assert_equal form.errors.full_messages_for('band.name'), ["Band Name must be filled"] + assert_equal form.band.errors.full_messages_for(:name), ["Name must be filled"] + end + end + describe "#add" do let(:album_title) { nil } it do