哦,看起来,我孤单地解决了这一切……后一种选择确实比尝试在特定字段上查找和调用验证例程简单得多。更容易消除错误:
设置所有可能需要的字段,required=True
然后form.clean()
测试另一个字段的值,如有必要,只需从self.errors
# payment type
payment_method = forms.CharField(max_length="20", required=True)
payment_method.widget=forms.RadioSelect(choices=PAYMENT_METHOD_CHOICES)
# credit card details
cc_number = CreditCardField(max_length=20, required=True)
cc_name = forms.CharField(max_length=30, required=True)
cc_expiry = ExpiryDateField(required=True)
cc_ccv = VerificationValueField(required=True)
def clean(self):
data = super(PaymentForm, self).clean()
if data.get('payment_method') == 'paypal':
for field_name in ['cc_number','cc_name','cc_expiry','cc_ccv']:
if field_name in self.errors:
del self.errors[field_name]