def validate()

in django_airavata/apps/auth/serializers.py [0:0]


    def validate(self, attrs):
        ext_user_profile_field = attrs['ext_user_profile_field']
        # validate that id_value is only provided for choice fields, and 'text_value' only for the others
        if ext_user_profile_field.field_type == 'single_choice':
            choices = attrs.get('choices', [])
            other_value = attrs.get('other_value', '')
            # Check that choices are valid
            for choice in choices:
                if not ext_user_profile_field.single_choice.choices.filter(id=choice, deleted=False).exists():
                    raise serializers.ValidationError({'choices': 'Invalid choice.'})
            if len(choices) > 1:
                raise serializers.ValidationError({'choices': "Must specify only a single choice."})
            if len(choices) == 1 and other_value != '':
                raise serializers.ValidationError("Must specify only a single choice or the other choice, but not both.")
            if len(choices) == 0 and other_value == '':
                raise serializers.ValidationError("Must specify one of a single choice or the other choice (but not both).")
        elif ext_user_profile_field.field_type == 'multi_choice':
            choices = attrs.get('choices', [])
            other_value = attrs.get('other_value', '')
            # Check that choices are valid
            for choice in choices:
                if not ext_user_profile_field.multi_choice.choices.filter(id=choice, deleted=False).exists():
                    raise serializers.ValidationError({'choices': 'Invalid choice.'})
        return attrs