Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement request: require a or b but not both #90

Open
jvanasco opened this issue Aug 14, 2015 · 2 comments
Open

enhancement request: require a or b but not both #90

jvanasco opened this issue Aug 14, 2015 · 2 comments

Comments

@jvanasco
Copy link
Contributor

There doesn't seem to be a way to require A or B, but not A&B.

This could be achieved with RequireEmptyIfPresent/RequireEmptyIfMissing, which would be the same logic as RequireIfMissing - but tests for an empty value.

@jvanasco

This comment has been minimized.

@jvanasco
Copy link
Contributor Author

After toying around, I found this approach better:

class OnlyOneOf(FormValidator):
    # Field that only one of is allowed
    only_one_ofs = None
    not_empty = None
    __unpackargs__ = ('only_one_ofs', )

    messages = {
        'empty': _("You must submit one and only one of these linked fields."),
        'invalid': _("You may submit only one of these linked fields."),
    }

    def _to_python(self, value_dict, state):
        is_empty = self.field_is_empty
        presence = [not is_empty(value_dict.get(field)) for field in self.only_one_ofs]
        total_present = presence.count(True)
        if not total_present and self.not_empty:
            raise Invalid(
                _('You must provide a value for one of the fields: %s') % ', '.join(["`%s`" % field for field in self.only_one_ofs]),
                value_dict,
                state,
                error_dict = dict([(field,
                                    Invalid(self.message('empty', state),
                                            value_dict.get(field),
                                            state
                                            )
                                    )
                                    for field in self.only_one_ofs
                                   ]
                                  )
            )
        if total_present > 1:
            raise Invalid(
                _('You may only provide a value for one of the fields: %s') % ', '.join(["`%s`" % field for field in self.only_one_ofs]),
                value_dict,
                state,
                error_dict = dict([(field,
                                    Invalid(self.message('invalid', state),
                                            value_dict.get(field),
                                            state
                                            )
                                    )
                                    for field in self.only_one_ofs
                                   ]
                                  )
            )
        return value_dict
  • updated 2015/09/11 - supply True to the not_empty kwarg (defaults to None) will control when errors are raised. This allows for the fieldset to be optional or required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant