#582 ✓not-applicable
Andrea Fazzi

[PATCH] ValidationErrors#each

Reported by Andrea Fazzi | October 2nd, 2008 @ 11:55 AM | in 0.10.0

The attached patch contains a modified version of Validate::ValidationErrors#each. This new version allows the iteration through field/error_message pairs instead of yielding to the block the error message value only. I found this fix useful when I need to compose a json response that contains field/error_message pairs as in the example below:

  errors = model.errors.collect { |field, msg| { :id => field, :message => msg } }

  display(:success => true, :errors => errors)


Comments and changes to this ticket

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) December 12th, 2008 @ 04:10 PM

    • Assigned user cleared.
    • Tag changed from dm-more, dm-validations, patch to dm-more, dm-validations, patch
    • State changed from “new” to “hold”

    Andrea, there might be alot of code out that relies on the behavior of ValidationErrors#each yielding to the message only.

    Is there a way you can make the code backwards compatible, perhaps with a warning? You could look at the airty of the block, and if it accepts 1 argument, send it the message (and warn), otherwise pass it the field and message arguments.

  • Dan Kubb (dkubb)

    Dan Kubb (dkubb) January 8th, 2009 @ 02:48 PM

    • Assigned user set to “Michael Klishin (antares)”
  • Michael Klishin (antares)

    Michael Klishin (antares) March 25th, 2009 @ 03:39 PM

    • Milestone set to 0.10.0
    • State changed from “hold” to “not-applicable”

    After thinking about this for a while, I have to say lets keep old behavior. Error serialization is a hot topic anyway, and it quite easy to tame with a module that overrides to_json or what you need and calls super before or after it does specific work. At least this is what my fellow UI engineer does in Merb controllers.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »