Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add 'field' attribute to ActiveRecord::Errors#to_xml

#2523

Currently the output of model.erros.to_xml is:


<errors>
  <error>First name can't be blank</error>
  <error>Content Empty</error>
</errors>

This way it is a bit hard to parse which attribute the error is about (ex. in the first error above the attribute is acctually 'first_name'...)

The included diff file contains a fix to change it to:


<errors>
  <error field="first_name">First name can't be blank</error>
  <error field="content">Content Empty</error>
</errors>

This way nothing needs to be changed in current applications and the patch is safe.

Reported by neer · April 19th, 2009 @ 03:48 PM

State: stale
Milestone: 3.x
Assigned to: nobody
Importance: none

Activity

  1. labria
    labria

    Yes, that would be quite useful, +1 on that!

    April 19th, 2009 @ 03:52 PM

  2. Max Lapshin
    Max Lapshin
    • Tag changed from 2-3-stable, 3.0, activerecord, active_record, to_xml, xml to 2-3-stable, 3.0, activerecord, active_record, feature, test, to_xml, xml

    I've found this patch rather useful and we've tested it in our RIA, that can now highlight fields.

    April 20th, 2009 @ 07:25 PM

  3. bluefuton
    bluefuton

    +1 - currently doing this manually and it'd be be great if it was the default behaviour.

    November 12th, 2009 @ 02:39 PM

  4. David Trasbo
    David Trasbo

    +1

    This would definitely be a useful change. Unfortunately the patch no longer applies because the Errors class has been moved to ActiveModel.

    I've made a new patch which applies to the master branch at the time of writing. I've named the attribute that specifies which field the error message applies to "on", but feedback on that choice is welcome.

    April 14th, 2010 @ 06:10 PM

  5. David Trasbo
    David Trasbo

    Oops. Looks like I forgot to attach the patch. Here you go:

    April 14th, 2010 @ 06:11 PM

  6. Martin Andert
    Martin Andert

    See also Ticket #1625 Allow active record errors to_xml to return separate tags for attribute and m....

    Instead of

    <error field="...">...</error>
    

    I would prefer

    <error on="...">...</error>
    

    like in error_messages_on.

    April 14th, 2010 @ 07:54 PM

  7. subimage
    subimage

    +1 on this. It's puzzling that errors.to_json properly shows the field names, but not to_xml.

    Was going to look into implementing this for my API, but I'll apply the patch instead. Thanks!

    April 27th, 2010 @ 09:03 AM

  8. Neeraj Singh
    Neeraj Singh

    +1 the patch works.

    I agree with Martin that the attribute name should be 'field' instead of 'on'.

    April 29th, 2010 @ 04:39 AM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  10. zhshxa
    zhshxa
    • Importance changed from to

    David Trasbo,

    The last patch does not work.

    I must made below change:

      options[:builder].errors do |e|
        self.each do |attr, messages|
          Array.wrap(messages).each do |msg|
            e.error(msg, :on => attr)
          end
        end
      end
    

    -Sandy

    October 2nd, 2010 @ 05:10 AM

  11. Ryan Bigg
    Ryan Bigg
    • Tag cleared.

    Automatic cleanup of spam.

    October 11th, 2010 @ 10:58 AM

  12. Ryan Bigg
    Ryan Bigg

    Automatic cleanup of spam.

    October 19th, 2010 @ 08:26 AM

  13. rails
    rails
    • State changed from new to open

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    April 30th, 2011 @ 01:00 AM

  14. rails
    rails
    • State changed from open to stale

    April 30th, 2011 @ 01:00 AM

  15. bluefuton
    bluefuton
    • Tag set to activerecord

    This issue still exists in Rails 3.0.7. It's easy to retrieve the attribute name from the to_json output, but it is not included when using to_xml.

    >> zebra.errors
    => #<OrderedHash {:name=>["can't be blank"]}>
    >> zebra.errors.to_xml
    => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<errors>\n  <error>Name can't be blank</error>\n</errors>\n"
    >> zebra.errors.to_json
    => "{\"name\":[\"can't be blank\"]}"
    

    [state:open]

    May 3rd, 2011 @ 10:21 AM