Cloudflare Turnstile for Symfony

A simple package to help integrate Cloudflare Turnstile on Symfony Form.

Cloudfare Turnstile is an alternative to Google reCaptcha.

Last updated: 2023-12-08

Cloudflare Turnstile for Symfony

Version

0.3.0

symfony

5,6,7

PHP

>= 7.4

Contributors

4

Installation

You can install the package via Composer:

composer require pixelopen/cloudflare-turnstile-bundle

Add bundle into config/bundles.php file :

PixelOpen\CloudflareTurnstileBundle\PixelOpenCloudflareTurnstileBundle::class => ['all' => true]

Add a config file into config/packages/pixelopencloudflare_turnstile.yaml :

pixel_open_cloudflare_turnstile:
  key: '%env(TURNSTILE_KEY)%'
  secret: '%env(TURNSTILE_SECRET)%'

Visit Cloudflare to create your site key and secret key and add them to your .env file.

TURNSTILE_KEY="1x00000000000000000000AA"
TURNSTILE_SECRET="2x0000000000000000000000000000000AA"

Use with your Symfony Form

Create a form type and insert an Turnstile Type to add a Cloudflare Turnstile :

<?php

namespace App\Form;

use App\Entity\Contact;
use PixelOpen\CloudflareTurnstileBundle\Type\TurnstileType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ContactType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name', TextType::class, ['label' => false, 'attr' => ['placeholder' => 'name']])
            ->add('message', TextareaType::class, ['label' => false, 'attr' => ['placeholder' => 'message']])
            ->add('security', TurnstileType::class, ['attr' => ['data-action' => 'contact', 'data-theme' => 'dark'], 'label' => false])
            ->add('submit', SubmitType::class)
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Contact::class,
        ]);
    }
}

Testing

Use the following sitekeys and secret keys for testing purposes:

Sitekey

Always passes:

1x00000000000000000000AA

Always blocks

2x00000000000000000000AB

Forces an interactive challenge:

3x00000000000000000000FF

Changelog

0.3.0 (08/12/2023)

  • Allow Symfony 7 (thank you HeahDude)

0.2.0 (31/10/2023)

  • Add enable option

0.1.4 (07/07/2023)

  • Prefer defer to async to improve page speed #2 (thank Huluti)

  • add codacy

  • add security checker on github actions

  • add phpstan on github actions

  • add github actions

0.1.3 (05/12/2022)

  • Add explicit return type to avoid deprecation warnings on Symfony 6.2 #1 (thank you shmshd)

0.1.2 (10/22/2022)

  • Complete documentation

0.1.1 (10/22/2022)

  • Add recipe

  • Change namespace

  • Fix installation on readme file

0.1.0 (10/22/2022)

  • First release

Contributors