Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not getting "This field is required." if captcha field is left unchecked. #26

Open
tubescreamer84 opened this issue Nov 16, 2018 · 2 comments

Comments

@tubescreamer84
Copy link

Similar to #5

Implemented wagtail-django-recaptcha using the first method in the documentation. Captcha field seems to function except I don't get the red "This field is required." when captcha field is left unchecked. I do have a custom serve method on my form page model but I have tried commenting this custom function out and it doesn't seem to make a difference.

I apologize in advance as this is probably operator error but I have gone through the madewithwagtail.org repo and can't seem to find any obvious cause.

Running wagtail 2.2

models.py code:

`class FormField(AbstractFormField):
page = ParentalKey('FormPage', on_delete=models.CASCADE, related_name='form_fields')

class FormPage(WagtailCaptchaEmailForm):
hero_image = models.ForeignKey(
'wagtailimages.Image', null=True, blank=True,
on_delete=models.SET_NULL, related_name='+'
)

intro = RichTextField(blank=True)
thank_you_text = RichTextField(blank=True)

content_panels = AbstractEmailForm.content_panels + [
    ImageChooserPanel('hero_image'),
    FieldPanel('intro', classname="full"),
    InlinePanel('form_fields', label="Form fields"),
    FieldPanel('thank_you_text', classname="full"),
    MultiFieldPanel([
        FieldRowPanel([
            FieldPanel('from_address', classname="col6"),
            FieldPanel('to_address', classname="col6"),
        ]),
        FieldPanel('subject'),
    ], "Email"),
]

def serve(self, request, *args, **kwargs):
    print('CONTACT FORM SUBMISSION')
    sku = ''
    model = ''
    if request.method == 'POST':
        form = self.get_form(request.POST, page=self, user=request.user)

        if form.is_valid():
            print('Form Submitted')
            self.process_form_submission(form)

            # Update the original landing page context with other data
            # landing_page_context = self.get_context(request)
            
            context = self.get_context(request)
            context['message'] = 'Success message has been sent.'
            form = self.get_form(page=self, user=request.user)
            context['form'] = form
            return render(
                request,
                self.get_template(request),
                context
            )
    else:
        form = self.get_form(page=self, user=request.user)
        sku = request.GET.get('sku', False)
        model = request.GET.get('model', False)

#     model = request.GET.get('model')
    context = self.get_context(request)
    context['form'] = form
    context['sku'] = sku
    context['model'] = model
    return render(
        request,
        self.get_template(request),
        context
    )

def send_mail(self, form):
    # `self` is the FormPage, `form` is the form's POST data on submit

    # Email addresses are parsed from the FormPage's addresses field
    addresses = [x.strip() for x in self.to_address.split(',')]

    # Subject can be adjusted, be sure to include the form's defined subject field
    submitted_date_str = date.today().strftime('%x')
    subject = self.subject  # add date to email subject

    content = []

    # Add a title (not part of original method)
    content.append('{}: {}'.format('Form', self.title))

    for field in form:
        # add the value of each field as a new line
        value = field.value()
        if field.name == 'email':
            email = value
            print('EMAIL FIELD: ', value)
        if isinstance(value, list):
            value = ', '.join(value)
        content.append('{}: {}'.format(field.label, value))

    # Add a link to the form page
    content.append('{}: {}'.format('Submitted Via', self.full_url))

    # Add the date the form was submitted
    content.append('{}: {}'.format('Submitted on', submitted_date_str))

    # Content is joined with a new line to separate each text line
    content = '\n'.join(content)

    # wagtail.wagtailadmin.utils - send_mail function is called
    # This function extends the Django default send_mail function
    # send_mail(subject, content, addresses, self.from_address)
    send_mail(subject, content, addresses, email)`

Dockerfile:

RUN pip install whitenoise
RUN pip install gunicorn
RUN pip install gevent
RUN pip install 'Django>=2.0,<2.1'
RUN pip install 'wagtail>=2.2,<2.3'
RUN pip install 'django-debug-toolbar'
RUN pip install 'psycopg2~=2.6'
RUN pip install 'elasticsearch==6.0.0,<6.3.0'
RUN pip install 'django_sendmail_backend'
RUN pip install 'django_compressor==2.2'
RUN pip install 'django-libsass==0.7'
RUN pip install 'django-recaptcha'
RUN pip install 'wagtail-django-recaptcha'
RUN pip install 'django-honeypot==0.7.0'
RUN pip install geopy

base.py installed_apps and recaptcha settings:

`INSTALLED_APPS = [
'home',
'products',
'search',
'pages',

'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.contrib.settings',
'wagtail.search',
'wagtail.admin',
'wagtail.contrib.modeladmin',
'wagtail.core',
'compressor',
'modelcluster',
'taggit',
'honeypot',
# 'antispam',
'captcha',
'wagtailcaptcha',

# 'antispam.akismet',
# 'antispam.honeypot',
# 'antispam.captcha',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'debug_toolbar',

]

RECAPTCHA_PUBLIC_KEY = Actual public key
RECAPTCHA_PRIVATE_KEY = Actual private key
NOCAPTCHA = True
`

@tubescreamer84
Copy link
Author

Seems this is because of the way I am rendering the form in the template. If I render the form using {{ form.as_p }} the field required message displays as expected. If I render each field separately as shown below the error message doesn't work.

Wondering if there is a way around this.

`


{% csrf_token %}

Name:
{{ form.name }}

                    <div class="field">
                        <label class="label" for="{{ form.email.id_for_label }}">Email Address: </label>
                        <div class="control">{{ form.email }}</div>
                    </div>
                
                    <div class="field">
                            <label class="label" for="id_message">Message:</label>
                            <div class="control"> <textarea name="message" cols="40" rows="10" required="" id="id_message" placeholder="Message">{% if sku or model %}I would like to reserve {{ model }} - sku: {{ sku }}{% endif %}</textarea></div>
                    </div>
                  
                    <div class="field"><div class="control">{{ form.wagtailcaptcha }}</div></div>
               
                    <div class="field is-grouped">
                            <div class="control">
                            <button class="button">Submit</button>
                            </div>
                    </div>
                </form>`

@noobmaster19
Copy link

any updates regarding this? Im using crispy forms and im facing the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants