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.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to