I've written a series of RegexValidator definitions that I call on django model inputs. Here is an example:
def fein_validator(value):
err = None
for validator in FEIN_VALIDATOR:
try:
validator(value)
return value
except ValidationError as exc:
err = exc
raise err
For reference the FEIN_VALIDATOR
for this method is below. Note that in this example there is only a single item, I have other validators that have multiple items (hence the for loop
):
FEIN_VALIDATOR = [
RegexValidator(r'^\d{2}-\d{7}$')
]
The method works perfectly and throws an error when it's supposed to. But, the error it throws is Enter a valid value.
and I would like to customize the return to be more specific.
I've tried versions of this and this. But these all assume that there is only one pass. I'm trying to run through a series of validators using a for loop
.
Question 1: does the method construction I'm using work for this - or should there be a separate method for each validation? [whereby I can add custom messaging.]
Question 2: if this does work, how do I change the error message raised to a custom message?