From eedfb4c9f200e6a600fffed09d3115d9a8845cf2 Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sun, 29 May 2022 21:04:28 +0200 Subject: [PATCH] add captcha --- assets/css/app.scss | 29 ++++++++++++++++++++++++ composer.json | 1 + config/bundles.php | 1 + src/Form/Blog/UserCommentType.php | 14 ++++++++++++ src/Form/ContactType.php | 14 ++++++++++++ templates/page/contact/default.html.twig | 4 ++++ templates/page/post/_post.html.twig | 5 +++- 7 files changed, 67 insertions(+), 1 deletion(-) diff --git a/assets/css/app.scss b/assets/css/app.scss index bd9387b..5c29e82 100644 --- a/assets/css/app.scss +++ b/assets/css/app.scss @@ -109,6 +109,35 @@ pre[class*="language-"] { textarea { border-radius: 10px; } + + &-captcha { + label { + display: block; + } + + img { + vertical-align: middle; + margin-right: 10px; + border-radius: 10px; + width: auto; + } + + input { + display: inline-block; + width: 100px !important; + height: 50px !important; + margin-bottom: -1px; + } + + .form-error-icon { + display: none; + } + + .form-error-message { + display: block; + color: red; + } + } } .button, .btn, input[type="submit"], input[type="button"] { diff --git a/composer.json b/composer.json index d8220c2..b85a81b 100644 --- a/composer.json +++ b/composer.json @@ -7,6 +7,7 @@ "php": ">=8.0.0", "beberlei/doctrineextensions": "^1.3", "friendsofsymfony/jsrouting-bundle": "^2.7", + "gregwar/captcha-bundle": "^2.2", "knplabs/knp-markdown-bundle": "^1.9", "knplabs/knp-menu-bundle": "^3.1", "murph/murph-core": "dev-master" diff --git a/config/bundles.php b/config/bundles.php index 66f1912..7af1264 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -24,4 +24,5 @@ return [ Knp\DoctrineBehaviors\DoctrineBehaviorsBundle::class => ['all' => true], SPE\FilesizeExtensionBundle\SPEFilesizeExtensionBundle::class => ['all' => true], Liip\ImagineBundle\LiipImagineBundle::class => ['all' => true], + Gregwar\CaptchaBundle\GregwarCaptchaBundle::class => ['all' => true], ]; diff --git a/src/Form/Blog/UserCommentType.php b/src/Form/Blog/UserCommentType.php index efe5d8b..dcbd122 100644 --- a/src/Form/Blog/UserCommentType.php +++ b/src/Form/Blog/UserCommentType.php @@ -15,6 +15,7 @@ use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Url; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; +use Gregwar\CaptchaBundle\Type\CaptchaType; class UserCommentType extends AbstractType { @@ -86,6 +87,19 @@ class UserCommentType extends AbstractType ] ); + $builder->add( + 'captcha', + CaptchaType::class, + [ + 'invalid_message' => 'Code invalide', + 'label' => 'Captcha', + 'max_front_lines' => 1, + 'width' => 140, + 'height' => 50, + 'length' => 3, + ] + ); + $builder->add( 'parentCommentId', HiddenType::class, diff --git a/src/Form/ContactType.php b/src/Form/ContactType.php index 817472f..e9a75ef 100644 --- a/src/Form/ContactType.php +++ b/src/Form/ContactType.php @@ -10,6 +10,7 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\NotBlank; +use Gregwar\CaptchaBundle\Type\CaptchaType; class ContactType extends AbstractType { @@ -71,6 +72,19 @@ class ContactType extends AbstractType ], ] ); + + $builder->add( + 'captcha', + CaptchaType::class, + [ + 'invalid_message' => 'Code invalide', + 'label' => 'Captcha', + 'max_front_lines' => 1, + 'width' => 140, + 'height' => 50, + 'length' => 3, + ] + ); } public function configureOptions(OptionsResolver $resolver) diff --git a/templates/page/contact/default.html.twig b/templates/page/contact/default.html.twig index 65cb55b..661a1b1 100644 --- a/templates/page/contact/default.html.twig +++ b/templates/page/contact/default.html.twig @@ -36,6 +36,10 @@ {{ form_errors(form.message) }} {{ form_widget(form.message, {attr: {cols: 30, rows: 10}}) }} +
+ {{ form_label(form.captcha) }} + {{ form_widget(form.captcha) }} +
diff --git a/templates/page/post/_post.html.twig b/templates/page/post/_post.html.twig index 8f488d7..79751bd 100644 --- a/templates/page/post/_post.html.twig +++ b/templates/page/post/_post.html.twig @@ -180,7 +180,10 @@
- +
+ {{ form_label(form.captcha) }} + {{ form_widget(form.captcha) }} +