def _is_choice_selected(field, value, choice): """Checks if a choice is selected. If the field is a multi select field it's checked if the choice is in the passed iterable of values, otherwise it's checked if the value matches the choice. """ if field.multiple_choices: for value in value: if _value_matches_choice(value, choice): return True return False return _value_matches_choice(value, choice)
def convert(self, value): if not value and not self.required: return if self.choices: for choice in self.choices: if isinstance(choice, tuple): choice = choice[0] if _value_matches_choice(value, choice): return choice message = self.messages['invalid_choice'] if message is None: message = self.gettext('Please enter a valid choice.') raise ValidationError(message)