--
Ticket URL: <https://code.djangoproject.com/ticket/17413>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* stage: Unreviewed => Design decision needed
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:1>
* stage: Design decision needed => Accepted
Comment:
Would be nice to have.
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:2>
Comment (by merb):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:3>
* owner: nobody => merb
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:4>
* needs_docs: 0 => 1
* has_patch: 0 => 1
* needs_tests: 0 => 1
* easy: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:5>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:6>
Comment (by merb):
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()
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:7>
Comment (by loic84):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:8>
Comment (by merb):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:9>
Comment (by loic84):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:10>
Comment (by loic84):
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:11>
* owner: merb => loic84
* version: => master
* easy: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:12>
Comment (by loic84):
Following the merge of #20867, I've been able to resume work on this PR:
https://github.com/django/django/pull/1483
Feedback welcome.
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:13>
* needs_docs: 1 => 0
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:14>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"3ce9829b615336b0f3ac39b080c27fc8cf5af483"]:
{{{
#!CommitTicketReference repository=""
revision="3ce9829b615336b0f3ac39b080c27fc8cf5af483"
Fixed #17413 -- Serialization of form errors along with all metadata.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:15>
Comment (by Tim Graham <timograham@…>):
In [changeset:"2fd7fc134cf0c0685ceac22fd858509aa43f819f"]:
{{{
#!CommitTicketReference repository=""
revision="2fd7fc134cf0c0685ceac22fd858509aa43f819f"
Refs #17413 -- Added isinstance backward compatibility on ErrorList.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:16>
Comment (by Florian Apolloner <florian@…>):
In [changeset:"34236efc5e569ce7d42f7d52dd798e59f95457f8"]:
{{{
#!CommitTicketReference repository=""
revision="34236efc5e569ce7d42f7d52dd798e59f95457f8"
Reworked ErrorDict.as_json() to prevent unnecessary
serialization/deserialization step.
Thanks @apollo13 for the suggestion. Refs #17413.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/17413#comment:17>