Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Form Validation in Django-Allauth

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?

like image 259
CraigH Avatar asked Apr 25 '14 23:04

CraigH


People also ask

What is form validation in Django?

We had a basic introduction to Django's Forms and Model forms in my previous post. We will now discuss in detail the validation of form data. Form validation is the main reason that any developer has to use Forms for. The basic security principle of any computer application is "do not trust the user input".

How to configure Django-allauth to display the registration form?

Follow the URL patterns to display the registration form. Eg: Visit localhost:8000/accounts/login to display the login page. Most django-allauth features are can be configured using the built-in adapters and variables by placing them in the file settings.py.

How to check the status of a form field in Django?

We can check them in the Django shell which we can start with: Once the shell is up and running, do the following. We have only checked the status of the first_name form field. All the other fields are also similar. There is only one validator associated with the first_name field and that is MaxLengthValidator.

How to set up social login in Django using Django-allauth?

Create a Django project if you already don’t have one. Install django-allauth using the command pip install django-allauth Add, allauthallauth.account, allauth.socialaccount and all the social login features you need to INSTALLED_APPS section in settings.py. You can view the entire list of supported API's here.


1 Answers

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

like image 168
jordiburgos Avatar answered Oct 08 '22 13:10

jordiburgos