Scenario: I'm building an order-form. Like every other order-form on the planet, it has separate invoicing shipping addresses. I've just added a "Use billing address" checkbox to let the user save time.
The problem is, the shipping fields are still there. They will fail validation if the user don't enter any shipping address data (like if they want to use the billing address).
What I think I'd like to do override the ModelForm validation for these duplicate fields. In there, if the box is checked (not sure how I get that data from within a validator), I return the billing version. If it's not checked, I pass it back to the original validation.
Sounds like a plan doesn't it? Well I fell at the first hurdle. My clean_functions
aren't working. Doesn't look like they're even being called.
Here's some code:
# shipping_street is a field in my Order Model
class OrderForm(ModelForm):
class Meta:
model = Order
def clean_shipping_street(self):
print "JUST GET ME SOME OUTPUT!!!"
raise forms.ValidationError('RAWRAWR')
Here's how I'm testing:
def checkout(request):
of = OrderForm()
if request.method == "POST":
of = OrderForm(request.POST)
print 'Form valid:', of.is_valid()
# ...
# return my HttpResponse with 'of' in the context.