This is my first time using custom validators, so I want to make sure I'm on the right track.
Here's a stripped down version of my model and the validation requirements: class Event(models.Model): date = models.DateField() recurrence = models.SmallIntegerField(blank=True, null=True, choices=RECURRENCE_CHOICES) every = models.PositiveSmallIntegerField(blank=True, null=True) end_date = models.DateField(blank=True, null=True) recurrence will be null, or a number between 1 and 4. If recurrence is not null, the every and end_date fields must be provided, and end_date must be greater than date. So here is my model after I added in all the validation. It works great right now... I'm just wondering if there is a better/easier way to do this? It didn't look like I could meet my needs with just the validators in django.core.validators, so I wrote a couple to help me out. class ValidateIfOtherFieldGiven(object): def __init__(self, other_field, validator_list): self.other_field, self.validator_list = other_field, validator_list self.always_test = True def __call__(self, field_data, all_data): if all_data.get(self.other_field, False): for v in self.validator_list: v(field_data, all_data) class IsGreaterThanOtherField(object): def __init__(self, other_field, error_message): self.other_field, self.error_message = other_field, error_message def __call__(self, field_data, all_data): if field_data < all_data[self.other_field]: raise validators.ValidationError, self.error_message recurrence_validator = validators.RequiredIfOtherFieldGiven('recurrence', 'This field is required for recurring events') end_date_greater_validator = IsGreaterThanOtherField('date', 'End date must be later than Date') every_validator = ValidateIfOtherFieldGiven('recurrence', [recurrence_validator]) end_date_validator = ValidateIfOtherFieldGiven('recurrence', [recurrence_validator, end_date_greater_validator]) class Event(models.Model): date = models.DateField() recurrence = models.SmallIntegerField(blank=True, null=True, choices=RECURRENCE_CHOICES) every = models.PositiveSmallIntegerField(blank=True, null=True, validator_list=[every_validator]) end_date = models.DateField(blank=True, null=True, validator_list=[end_date_validator]) Also, would it be worth submitting the two validators as a patch? It seems like ValidateIfOtherFieldGiven could be useful for others as well, and IsGreaterThanOtherField is a logical opposite for the already-existing IsLessThanOtherField. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---