from django import forms
from django.contrib.auth.models import User
from django.forms.utils import ErrorList


class UserForm(forms.Form):
    name = forms.CharField(required=True)
    email = forms.EmailField(required=True)
    password = forms.CharField(required=True)
    phone = forms.CharField(required=True)
    name_company = forms.CharField(required=True)

    def is_valid(self):
        valid = True
        if not super(UserForm, self).is_valid():
            self.add_erro('Por favor, verifique os dados informados')
            valid = False
        user_exists = User.objects.filter(username=self.data['name']).exists()
        if user_exists:
            self.add_erro('Usuario ja existente')
            valid = False

        return valid

    def add_erro(self, message):
        self._errors.setdefault(forms.forms.NON_FIELD_ERRORS, ErrorList()).append(message)

[(7, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (8, 'django.forms.EmailField', 'forms.EmailField', False, 'from django import forms\n'), (9, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (10, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (11, 'django.forms.CharField', 'forms.CharField', False, 'from django import forms\n'), (18, 'django.contrib.auth.models.User.objects.filter', 'User.objects.filter', False, 'from django.contrib.auth.models import User\n'), (26, 'django.forms.utils.ErrorList', 'ErrorList', False, 'from django.forms.utils import ErrorList\n')]