1

So I have the following form

class SignUpForm(UserCreationForm):
    email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={
        'class': tailwind_class
    }))
    password1 = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'class': tailwind_class}))
    password2 = forms.CharField(label='Confirm Password', widget=forms.PasswordInput(attrs={'class': tailwind_class}))
    company_id = forms.CharField(label='Company Identifier',
                                 widget=forms.PasswordInput(attrs={'class': tailwind_class}))

    class Meta:
        model = get_user_model()
        fields = ('email', 'password1', 'password2', 'company_id')

    # Custom validator for the company id
    def clean_company_id(self):
        company_id = self.cleaned_data['company_id']

        if not Company.objects.get(id=company_id):
            raise ValidationError("This Company ID doesn't exist")

        # Return value to use as cleaned data
        return company_id

whose validator works except of displaying the error message in case it doesn't validate.

That's my template:

<form class="form shadow-2xl min-w-[360px] max-w-[360px] p-4 bg-woys-purple rounded-lg text-white" method="post" action=".">
            {% csrf_token %}
            <div class="error-wrapper mb-2">
                <div class="errors text-woys-error-light text-center">{{ form.errors.email }}</div>
                <div class="errors text-woys-error-light text-center">{{ form.errors.password2 }}</div>
                <div class="errors text-woys-error-light text-center">{{ form.non_field_errors }}</div>
            </div>
...

1 Answer 1

2

This won't work.

When you do:

SomeModel.objects.get(...)

And the object doesn't exist within the get() filtet parameters, it will raise SomeModel.DoesNotExist so try changing the .get() to a filter(...).exists() and this way you will get your custom error instead.

I'm referring to changing this line of code:

if not Company.objects.get(id=company_id):

Furthermore, if you just want to update the error message, you can specify and override the messages for each of the fields and each type or error that can happen for that field type.

See this post which was answered previously which describes how to do it:

Create Custom Error Messages with Model Forms

Also I have just noticed that in your template you don't actually display the errors for that field so please add the row to display the field errors for company id

<div class="errors text-woys-error-light text-center">{{ form.errors.company_id }}</div>
Sign up to request clarification or add additional context in comments.

6 Comments

thanks -- I tried this but this doesnt show the validation error either
Do you get any field error for this field? If so, what error are you getting?
no, nothing pops up...
Please read my last edit. You aren't actually rendering this fields errors in your template html
but according to the docu validationErrors should be displayed via non_field_errors hmm -- docs.djangoproject.com/en/4.1/ref/forms/validation/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.