views:

16

answers:

2

Essentially I want to sanitize some data a user submits in a form when I redisplay it if there is an error. This is easy to do if I am extracting the data from a form object. I can override the clean() method and manipulate the data. I can also set the .initial value for the first time it is displayed. However, I cannot find a way of manipulating the form data that is going to redisplayed on error. For example, say a user submits a phone number of "123 456 test test 7890”, I want to be able to strip out the non-alphanumeric characters(that is easy) and show them just the numbers “1234567890” in the form field.

A: 

Perhaps you can run a script after the form is populated, extract the contents, clean them, and then re-insert them?

kanov-baekonfat
+1  A: 

The form's data should come from it's data variable, which is supposed to be a dictionary, so try eg. setting self.data['phone_numer'] = 123456789!

But if the data is coming from a request (which will be the case i guess) the dictonary will be a QueryDict, which is supposed to be immutable, so copy it first:

self.data = self.data.copy()
self.data['phone_number'] = 1234567890
lazerscience
You nailed it, thanks.
stinkypyper