Title
Rails 3 - Solution to Validation Error Messages twice
Body

Many times, in our Rails 3 applications while using dynamic forms we tend to see error messages twice one next to another... most annoying thing is that everything is right and nothing is wrong , but still rails 3 serves multiple messages.

We had a validation of email which gave us similar problem

validates :email, :uniqueness=>true, :allow_nil => false

It shouldn't have thrown multiple errors, but still did. We searched the internet but didnot find any solutions.

Evaluating the errors, we identified that the errors might be from dyanamic_form plugin which actually serializes the errors and their messages to the front end. So we thought about hacking dynamic form.

The solution worked well. we set a uniq in our errors and our dynamic errors vanished. The following are the changes we did in the dynamic_form plugin


# /vendor/plugins/dynamic_form/lib/action_view/helpers/dynamic_form.rb

Line: 225

object.errros.full_messages.map do |msg|

replaced to

object.errros.full_messages.uniq.map do |msg|

This eliminates the number of duplicate messages and displays only the ones in use. However this didnot eliminate the total count in the messages list. so we had to change in one another place

Line: 201

count = objects.inject(0) {|sum,object| sum + object.errors.full_messages.count }

replaced to

count = objects.inject(0) {|sum,object| sum + object.errors.full_messages.uniq.count }

This will solve your duplicate error messages problem.

Associated Tags
rails 3, dynamic_form, error messages, twice, duplicate
Created By
admin
Created At
2011-09-22 05:11:45 UTC
Last Updated At
2011-09-22 05:11:45 UTC
Add Comment