I'm trying to build a form in Django 1.5 and Python 2.7 that manage payments from ether paypal or credit/debit card (it's a dummy project so no real payments occurs).
I have two problems:
- first, the conditions that check the lenght f the card number and the ccv don't work.
- second, I have two form on the same page and I'd love to show errors only for the forms that was actualy compiled and ignore the empty one. Right now it show the errors for both of them, but I don'tknow if it's avoidable.
This are the forms:
class PaymentFormPaypal(forms.Form):
paypal = forms.EmailField(required = True, label = 'Your PayPal address')
class PaymentFormCard(forms.Form):
card = forms.IntegerField(required = True, label = 'Your card number')
ccv = forms.IntegerField(required = True, label = 'Card secure code')
expiration = forms.DateField(required = True, label = 'Card expiration date', widget=forms.extras.MonthYearWidget)
name = forms.CharField(max_length = 30, required = True, label = 'Name of the card owner')
surname = forms.CharField(max_length = 30, required = True, label = 'Surname of the card owner')
def clean_card(self):
card = self.cleaned_data['card']
if len(str(card)) != 16:
raise forms.ValidationError("The card number must be 16 digits long")
def clean_ccv(self):
ccv = self.cleaned_data['ccv']
if len(str(ccv)) != 3:
raise forms.ValidationError("The ccv must be 3 digits long")
widgets = {
'expiration': forms.extras.MonthYearWidget(),
}
this the view:
def payment(request, type):
if request.method == 'POST':
form1 = PaymentFormPaypal(request.POST)
form2 = PaymentFormCard(request.POST)
if form1.is_valid() or form2.is_valid():
profile = request.user.get_profile()
profile.premiumstatus = True
profile.save()
form = Search()
return render(request, 'home.html', {'form': form, 'request': request})
else:
form1=PaymentFormPaypal()
form2=PaymentFormCard()
return render(request, 'payment.html', {'form1': form1, 'form2': form2, 'request': request})
and this is the HTML page:
{% block content %}
<hr>
{% if request.user.is_authenticated %}
{% if form1.errors %}
<p style="color: red;">
Please correct the error{{ form1.errors|pluralize }} below.
</p>
{% endif %}
<p>Pay with PayPal.</p>
<form action="" method="post">
{% csrf_token %}
{{ form1.as_p }}
<input type="submit" value="Buy">
<input type="reset" value="Reset">
</form>
<p>Pay with your card.</p>
{% if form2.errors %}
<p style="color: red;">
Please correct the error{{ form2.errors|pluralize }} below.
</p>
{% endif %}
<form action="" method="post">
{% csrf_token %}
{{ form2.as_p }}
<input type="submit" value="Buy">
<input type="reset" value="Reset">
</form>
{% else %}
<p>You must be logged to make a payment!</p>
{% endif %}
<hr>
{% endblock %}
self.cleaned_data['ccv']etc.?self.cleaned_data['card']andself.cleaned_data['ccv']are integers (they are inserted in aforms.IntegerField). @dm03514: any number i set for both card and ccv raise the custom error inclean_card(self)andclean_ccv(self). And if I insert data in only one form the other form complain about it's fields being required.