Browse Source

[Frontend] Captcha

feature/module_traiteur_v1
Guillaume 3 years ago
parent
commit
772aa182f8
3 changed files with 48 additions and 2 deletions
  1. +21
    -1
      ShopBundle/Form/Frontend/NewsletterType.php
  2. +11
    -0
      ShopBundle/Form/Frontend/RegistrationType.php
  3. +16
    -1
      ShopBundle/Services/Utils.php

+ 21
- 1
ShopBundle/Form/Frontend/NewsletterType.php View File



namespace Lc\ShopBundle\Form\Frontend; namespace Lc\ShopBundle\Form\Frontend;


use Lc\ShopBundle\Services\UtilsManager;
use Lc\ShopBundle\Validator\Constraints\UniqueEmailValidator; use Lc\ShopBundle\Validator\Constraints\UniqueEmailValidator;
use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\EqualTo;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\NotNull;


class NewsletterType extends AbstractType class NewsletterType extends AbstractType
{ {
protected $utils ;

public function __construct(UtilsManager $utilsManager)
{
$this->utils = $utilsManager->getUtils() ;
}

public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder->add('email', EmailType::class, [ $builder->add('email', EmailType::class, [
'label' => 'Renseignez votre adresse email :'
'label' => 'Renseignez votre adresse email :',
'constraints' => [
new Email(),
new NotNull()
]
]); ]);

// captcha
$this->utils->addCaptchaType($builder);
} }


public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)

+ 11
- 0
ShopBundle/Form/Frontend/RegistrationType.php View File

namespace Lc\ShopBundle\Form\Frontend; namespace Lc\ShopBundle\Form\Frontend;


use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType; use FOS\UserBundle\Form\Type\RegistrationFormType as BaseRegistrationFormType;
use Lc\ShopBundle\Services\UtilsManager;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;


class RegistrationType extends AbstractType class RegistrationType extends AbstractType
{ {
protected $utils ;

public function __construct(UtilsManager $utilsManager)
{
$this->utils = $utilsManager->getUtils() ;
}

public function getParent() public function getParent()
{ {
return BaseRegistrationFormType::class; return BaseRegistrationFormType::class;
'mapped' => false, 'mapped' => false,
'translation_domain' => 'lcshop', 'translation_domain' => 'lcshop',
]); ]);

// captcha
$this->utils->addCaptchaType($builder);
} }





+ 16
- 1
ShopBundle/Services/Utils.php View File

use Lc\ShopBundle\Context\UserPointSaleInterface; use Lc\ShopBundle\Context\UserPointSaleInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager; use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpClient\HttplugClient; use Symfony\Component\HttpClient\HttplugClient;
use Symfony\Component\HttpFoundation\ParameterBag; use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Validator\Constraints\EqualTo;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;


class Utils class Utils
} }





public function addCaptchaType(FormBuilderInterface $builder)
{
$builder->add('specialField', HiddenType::class, [
'data' => 0,
'mapped' => false,
'attr' => [
'class' => 'special-field'
],
'constraints' => [
new EqualTo(['value' => $this->parameterBag->get('app.captcha_value'), 'message' => 'Valeur incorrecte'])
],
]);
}


} }

Loading…
Cancel
Save