#2472 closed defect (invalid)
forms get_validation_errors()
Reported by: | ratty | Owned by: | somebody |
---|---|---|---|
Component: | Validators | Version: | |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In the included code exerpt, I am setting default values for form fields I do not want the user to be able to modify, specifically 'user' and 'hits'. I wrote a routine to remove any values from request.POST I don't want the user to have control over. This is done by creating an instance of MultiValueDict and copying over any values that the user is allowed to post, returning the new MultiValDict instance.
### post_sanitize
def post_sanitize(post, fields):
safe_post = MultiValueDict({})
for f in fields:
if post.get(f):
safe_post[f] = post.get(f)
return safe_post
### excerpt from view.py
post_copy = request.POST.copy()
safe_post = post_sanitize(post_copy, valid_post_fields)
safe_postuser = 1
safe_posthits = 0
errors = manipulator.get_validation_errors(safe_post)
When safe_posthits is set to 0, I get an form error when I call get_validation_errors, saying that 'hits' is not set
If i set 'hits' to 1, I get this error:
/usr/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/forms/init.py in hasNoNewlines
- def hasNoNewlines(self, data, form):
- if data and '\n' in data:
Exception Type: TypeError
Exception Value: iterable argument required
Change History (2)
comment:1 by , 18 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 18 years ago
To get POST assignment working, the following has to hold:
- the assigned to field should NOT have Editable=False in the model
- you must assign string values, e.g.
new_data['myfield'] = str(1)
- the field is named according to model WITHOUT any internal prefixes like id
Until you call the manipulator's
do_html2python
method (which is to say, up until right before saving), it expects data to be in the format in which it would originally have been submitted; in this case, strings or lists of strings, because HTTP has no way of saying "thisPOST
value is anint
".