1from wtforms.fields import Field
2
3from . import widgets
4from .validators import Recaptcha
5
6__all__ = ["RecaptchaField"]
7
8
9class RecaptchaField(Field):
10 """reCAPTCHA field using :class:`.Recaptcha` as its default validator.
11
12 The default validator skips verification when ``current_app.testing`` is
13 ``True``, so tests don't need a real reCAPTCHA token.
14
15 When using a nonce-based Content Security Policy, pass ``nonce`` to
16 populate the ``nonce`` attribute of the generated ``<script>`` tag. A
17 zero-argument callable is accepted so the value can be resolved at
18 render time, e.g. ``nonce=lambda: g.csp_nonce``.
19 """
20
21 widget = widgets.RecaptchaWidget()
22
23 # error message if recaptcha validation fails
24 recaptcha_error = None
25
26 def __init__(self, label="", validators=None, nonce=None, **kwargs):
27 validators = validators or [Recaptcha()]
28 self.nonce = nonce
29 super().__init__(label, validators, **kwargs)