I've got a form to register clients. There I've got one ESIdentityCardNumberField to validate NIF/CIF/NIE, but this form is also considered to allow "Other" country Cards IDs, so I've added another field to get them.
So, I need to override ESIdentityCardNumberField validation to allow blank values if other card id is specified.
How can I get this?
class Client(models.Model):
name = models.CharField()
ESCardId = models.CharField(blank=True, null=True)
otherCardsIds = models.CharField(blank=True, null=True)
class ClientForm(forms.ModelForm):
ESCardId= ESIdentityCardNumberField()
class ClientAdmin(admin.ModelAdmin):
form = ClientForm
fields = ['name', 'ESCardID', 'otherCardsIds']
def save_model(self, request, obj, form, change):
if obj.ESCardId is None and obj.otherCardsIds is None:
raise ValidationError("Enter a spanish cardID or any other.")
Thanks in advance.
I have noticed bad point of view. Don't use a form field but a validator, like: