7

I want to do some extra validation on fields in django-allauth. For example I want to prevent using free email addresses. So I want to run this method on signup

def clean_email(self):
    email_domain = self.cleaned_data['email'].split('@')[1]
    if email_domain in self.bad_domains:
        raise forms.ValidationError(_("Registration using free email addresses is prohibited. Please supply a different email address."))

Similarly I want to run custom validation on different fields other than email address. How can I perform this?

CraigH
  • 2,041
  • 3
  • 30
  • 48

1 Answers1

7

There are some adapters on the allauth configuration. For example this one:

ACCOUNT_ADAPTER (="allauth.account.adapter.DefaultAccountAdapter")
    Specifies the adapter class to use, allowing you to alter certain default behaviour.

You can specify a new adapter by overriding the default one. Just override the clean_email method.

class MyCoolAdapter(DefaultAccountAdapter):

    def clean_email(self, email):
        """
        Validates an email value. You can hook into this if you want to
        (dynamically) restrict what email addresses can be chosen.
        """
        *** here goes your code ***
        return email

Then modify the ACCOUNT_ADAPTER on the settings.py

ACCOUNT_ADAPTER = '**app**.MyCoolAdapter'

Check the default behavior on: https://github.com/pennersr/django-allauth/blob/master/allauth/account/adapter.py

jordiburgos
  • 5,964
  • 4
  • 46
  • 80