This project is archived and is in readonly mode.

#2523 ✓stale
neer

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

Reported by neer | April 19th, 2009 @ 03:48 PM | in 3.x

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.

Comments and changes to this ticket

  • labria

    labria April 19th, 2009 @ 03:52 PM

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

  • Max Lapshin

    Max Lapshin April 20th, 2009 @ 07:25 PM

    • 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.

  • bluefuton

    bluefuton November 12th, 2009 @ 02:39 PM

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

  • David Trasbo

    David Trasbo April 14th, 2010 @ 06:10 PM

    +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.

  • David Trasbo

    David Trasbo April 14th, 2010 @ 06:11 PM

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

  • Martin Andert

    Martin Andert April 14th, 2010 @ 07:54 PM

    See also Ticket #1625.

    Instead of

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

    I would prefer

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

    like in error_messages_on.

  • subimage

    subimage April 27th, 2010 @ 09:03 AM

    +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!

  • Neeraj Singh

    Neeraj Singh April 29th, 2010 @ 04:39 AM

    +1 the patch works.

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

  • Jeremy Kemper

    Jeremy Kemper May 4th, 2010 @ 06:48 PM

    • Milestone changed from 2.x to 3.x
  • zhshxa

    zhshxa October 2nd, 2010 @ 05:10 AM

    • 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

  • Ryan Bigg

    Ryan Bigg October 11th, 2010 @ 10:58 AM

    • Tag cleared.

    Automatic cleanup of spam.

  • Ryan Bigg

    Ryan Bigg October 19th, 2010 @ 08:26 AM

    Automatic cleanup of spam.

  • rails

    rails April 30th, 2011 @ 01:00 AM

    • 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.

  • rails

    rails April 30th, 2011 @ 01:00 AM

    • State changed from “open” to “stale”
  • bluefuton

    bluefuton May 3rd, 2011 @ 10:21 AM

    • 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]

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages