Hi Andy, Yep, this little corner of the validator module is a bit messy. I ran into exactly the problems you describe. It turns out that Matt Rigott first reported problems in this area in June, along with a patch. I noticed in July, updated the patch, and added unit tests (very topical!)
I've been a bit wary of pestering Adrian, Jacob et. al. to apply the patch, given their current workloads. However, if you could have a look at the patch and comment and/or fix, then that would definitely help get these problems fixed. http://code.djangoproject.com/ticket/2266 Cheers, Alan. On 9/14/06, Andy Dustman <[EMAIL PROTECTED]> wrote: > > I noticed (after inferring it probably exists) the > RequiredIfOtherFieldsGiven validator, though it is currently > undocumented. The default error message is, "Please enter both fields > or leave them both empty." The actual test is: > > def __call__(self, field_data, all_data): > for field in self.other: > if all_data.get(field, False) and not field_data: > raise ValidationError, self.error_message > > Since field_data is a constant, this is equvalent to: > > def __call__(self, field_data, all_data): > if field_data: return > for field in self.other: > if all_data.get(field, False): > raise ValidationError, self.error_message > > i.e. if this field is set, then if any of the other fields are not > set, it's invalid. So the error message is wrong or there's a bug. > Based on the name of the validator, I think the problem is in the > error message. It should be more like "Please either enter this field > or leave the other ones empty." > > What I actually need for my app is OnlyIfOtherFieldsGiven, which in > fact is what the RequiredIfOtherFieldsGiven validation error claims to > be doing. The test for OnlyIfOtherFieldsGiven would be: > > def __call__(self, field_data, all_data): > for field in self.other: > if bool(all_data.get(field, False)) ^ bool(field_data): > raise ValidationError, self.error_message > > (Yes, bitwise-exclusive-or actually will work on booleans.) > > If there's some interest in actually having this in Django, let me > know and I'll write up a ticket with patch. > -- > This message has been scanned for memes and > dangerous content by MindScanner, and is > believed to be unclean. > > > > -- Alan Green [EMAIL PROTECTED] - http://bright-green.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@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-developers -~----------~----~----~----~------~----~------~--~---