Django validator error: 'list' object is not callable

I’m trying to add a validator to a contact form in forms.py to prevent spammers from adding http:// and https:// links in the message.

I’ve included from django.core.validators import RegexValidator at the top of forms.py.

In the example below, when I use the general_message form field and validators=, I get the error 'list’ object is not callable.

general_message = forms.CharField(label='General message*', required=True, widget=forms.Textarea, validators=[RegexValidator(regex=r'http(s)?|HTTP(s)?', message="No https allowed", code="invalid")] (
        attrs={'class': 'form-control', 'maxlength': '1000', 'rows': 8} ))

The example below of general_message works fine when not using the validator:

general_message = forms.CharField(label='General message*', required=True, widget=forms.Textarea(
        attrs={'class': 'form-control', 'maxlength': '1000', 'rows': 8}
    ))

Any ideas?

You’ve got some parenthesis mixed up here. Notice where your attrs attribute is relative to the widget and validators attributes.

Thanks, but I don’t see a difference in your code widget=forms.... and mine. Am I missing something?

I copied what you had - the mistake is where you have the validators attribute relative to the widget attribute. Compare this closely to the version that you say works.

How is the widget defined in the working version? How is it defined in the not working version?

Thanks, I edited my original question to show my original general_message field that works and the general_message field with the validator that throws the error.

Please post here, independently of what you’ve done before, just the widget attribute definition of the working version, and of the non-working version.

I don’t have a working version, other than a version that does not use the validator.

This is the non-working version:

general_message = forms.CharField(label='General message*', required=True, widget=forms.Textarea, validators=[RegexValidator(regex=r'http(s)?|HTTP(s)?', message="No https allowed", code="invalid")] (
        attrs={'class': 'form-control', 'maxlength': '1000', 'rows': 8} ))

This is what I mean by my reference to “working version”:

From these two versions, what is the widget attribute for each? I don’t want you to show the entire field definition, only the widget definition.