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)

[((151, 181), 'django.forms.CharField', 'forms.CharField', ([], {'required': '(True)'}), '(required=True)\n', (166, 181), False, 'from django import forms\n'), ((194, 225), 'django.forms.EmailField', 'forms.EmailField', ([], {'required': '(True)'}), '(required=True)\n', (210, 225), False, 'from django import forms\n'), ((241, 271), 'django.forms.CharField', 'forms.CharField', ([], {'required': '(True)'}), '(required=True)\n', (256, 271), False, 'from django import forms\n'), ((284, 314), 'django.forms.CharField', 'forms.CharField', ([], {'required': '(True)'}), '(required=True)\n', (299, 314), False, 'from django import forms\n'), ((334, 364), 'django.forms.CharField', 'forms.CharField', ([], {'required': '(True)'}), '(required=True)\n', (349, 364), False, 'from django import forms\n'), ((578, 625), 'django.contrib.auth.models.User.objects.filter', 'User.objects.filter', ([], {'username': "self.data['name']"}), "(username=self.data['name'])\n", (597, 625), False, 'from django.contrib.auth.models import User\n'), ((853, 864), 'django.forms.utils.ErrorList', 'ErrorList', ([], {}), '()\n', (862, 864), False, 'from django.forms.utils import ErrorList\n')]