Opened 13 years ago

Closed 11 years ago

Last modified 11 years ago

#17413 closed New feature (fixed)

Serialization or getting non-HTML version of form errors is not easy

Reported by: Mikhail Korobov Owned by: loic84
Component: Forms Version: dev
Severity: Normal Keywords: form errors serialization
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

See e.g. http://forrst.com/posts/Convert_Django_form_errors_to_JSON-ijw

What do you think about adding errors_dict attribute to BaseForm? A rough draft:

def _get_errors_dict(self):
    return dict(
        (k, map(unicode, v))
        for (k,v) in self.errors.iteritems()
    )
errors_dict = property(_get_errors_dict)

Attachments (1)

17413.diff (530 bytes ) - added by merb 11 years ago.

Download all attachments as: .zip

Change History (18)

comment:1 by Aymeric Augustin, 13 years ago

Triage Stage: UnreviewedDesign decision needed

comment:2 by Florian Apolloner, 12 years ago

Triage Stage: Design decision neededAccepted

Would be nice to have.

comment:3 by merb, 11 years ago

Wouldn't it be better to rewrite

class ErrorDict(dict)

and add as_json or as_dict?
Currently it has as_text and as_ul

This is how the ErrorDict got returned, so a simple

    @property
    def errors(self):
        "Returns an ErrorDict for the data provided for the form"
        if self._errors is None:
            self.full_clean()
        return self._errors

The next snippet would call the json.dumps of the error dict.

print json.dumps(form.errors.as_dict())

I apped a patch that would do the following. If this is wished we only need a small test.

I also made a Pull Request on github:
https://github.com/django/django/pull/1406

Last edited 11 years ago by merb (previous) (diff)

by merb, 11 years ago

Attachment: 17413.diff added

comment:4 by merb, 11 years ago

Owner: changed from nobody to merb
Status: newassigned

comment:5 by merb, 11 years ago

Easy pickings: set
Has patch: set
Needs documentation: set
Needs tests: set

comment:6 by merb, 11 years ago

Patch needs improvement: set

comment:7 by merb, 11 years ago

As i asked here: https://github.com/django/django/pull/1406
What's better, to have a errors_dict or a to_json() in the ErrorDict()

the first would make some things like this happen: json.dumps(errors_dict) the second would be form.errors.to_json()

comment:8 by loic84, 11 years ago

It's probably an unusual request (and hopefully an acceptable one) but I'd like to put the ErrorDict.to_json() effort on-hold.

I'll write more thoroughly about it next week on the ML but here is a quick explanation of my rationale.

Some background:

  • The ValidationError refactor [f34cfec0fa1243b4a3b9865b961a2360f211f0d8] enabled pertaining error metadata like error code and error params.
  • This fixed #20199 and #16986 by allowing metadata to flow freely between the Model layer and the Form layer.
  • My current work on #20867 will enable raising exceptions with all metadata from Form.clean().

The issue:

While ValidationError is now a capable carrier for errors with their metadata, all information is lost as soon as it reaches ErrorDict and ErrorList. I'd like to fix that by making those two classes capable of handling instances of ValidationError. This is the last piece of the puzzle, but it'll require a small backward incompatibility that will need to be discussed (basically changing the output of ValidationError.__str__) to allow Error(Dict|List) itself to remain backward compatible.

Once this has happened, to_json() would become much more useful because on top of having the error message, it will also have the error code, which will be very useful on the JS side. If we introduce to_json() now, we'll be unable to make this change because of backward compatibility.

comment:9 by merb, 11 years ago

Yeah that's okai for me. I had an working version these days, but my hard drive crashed. I first need to recover it. (and hope it will work) just write here when you are done.

comment:10 by loic84, 11 years ago

Some updates:

  • #20867 is shaping up nicely and provides the groundwork for this feature, @timo mentioned he'll review it once 1.6 has been released.
  • The experimental branch https://github.com/loic/django/compare/forms.errordict made ErrorDict aware of metadata, and implements as_json() to give a useful representation of the form errors along with all metadata (see test case).

This will be quite useful to anyone who want to further process the errors with JavaScript.

Now there are some remaining challenges in term of backward compatibility, I'll be focussing on that from now on.

comment:11 by loic84, 11 years ago

Now with backward compatibility: https://github.com/django/django/pull/1483.

Still missing tons of docs, especially since this builds on top of #20867 and we haven't decided yet on what will be the public API.

I also want to take this opportunity to create an exhaustive test suite for ErrorDict and ErrorList, these are tested indirectly by a gazillion of other tests, but I'd like to have a proper reference test suite for them.

comment:12 by loic84, 11 years ago

Easy pickings: unset
Owner: changed from merb to loic84
Version: master

comment:13 by loic84, 11 years ago

Following the merge of #20867, I've been able to resume work on this PR:

https://github.com/django/django/pull/1483

Feedback welcome.

comment:14 by Bouke Haarsma, 11 years ago

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:15 by Tim Graham <timograham@…>, 11 years ago

Resolution: fixed
Status: assignedclosed

In 3ce9829b615336b0f3ac39b080c27fc8cf5af483:

Fixed #17413 -- Serialization of form errors along with all metadata.

comment:16 by Tim Graham <timograham@…>, 11 years ago

In 2fd7fc134cf0c0685ceac22fd858509aa43f819f:

Refs #17413 -- Added isinstance backward compatibility on ErrorList.

comment:17 by Florian Apolloner <florian@…>, 11 years ago

In 34236efc5e569ce7d42f7d52dd798e59f95457f8:

Reworked ErrorDict.as_json() to prevent unnecessary serialization/deserialization step.

Thanks @apollo13 for the suggestion. Refs #17413.

Note: See TracTickets for help on using tickets.
Back to Top