From 60a04d4e6582233cf08fac0dfd5f4c52b5f4c8eb Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Thu, 19 May 2022 15:57:48 +0200 Subject: [PATCH] fix: Removed request option from Recaptcha constraint and using Form classes --- src/CustomForm/CustomFormHelper.php | 48 ----------------------------- src/Form/Constraint/Recaptcha.php | 4 +-- src/Form/CustomFormsType.php | 8 ++--- src/Mailer/ContactFormManager.php | 2 +- 4 files changed, 5 insertions(+), 57 deletions(-) diff --git a/src/CustomForm/CustomFormHelper.php b/src/CustomForm/CustomFormHelper.php index 7e8c006d..1bbd2616 100644 --- a/src/CustomForm/CustomFormHelper.php +++ b/src/CustomForm/CustomFormHelper.php @@ -5,7 +5,6 @@ namespace RZ\Roadiz\CoreBundle\CustomForm; use Doctrine\Persistence\ObjectManager; -use RZ\Roadiz\Core\AbstractEntities\AbstractField; use RZ\Roadiz\CoreBundle\Bag\Settings; use RZ\Roadiz\CoreBundle\Entity\CustomForm; use RZ\Roadiz\CoreBundle\Entity\CustomFormAnswer; @@ -69,7 +68,6 @@ public function getForm( return $this->formFactory->createNamed($name, CustomFormsType::class, $defaults, [ 'recaptcha_public_key' => $this->settingsBag->get('recaptcha_public_key'), 'recaptcha_private_key' => $this->settingsBag->get('recaptcha_private_key'), - 'request' => $request, 'customForm' => $this->customForm, 'forceExpanded' => $forceExpanded, ]); @@ -187,52 +185,6 @@ protected function getDocumentFolderForCustomForm(): ?Folder ); } - /** - * @param CustomFormAnswer|null $answer - * @param bool $forceExpanded - * @param array $options Options passed to final form - * - * @return FormInterface - * @throws \Exception - */ - public function getFormFromAnswer( - CustomFormAnswer $answer = null, - bool $forceExpanded = false, - array $options = [] - ): FormInterface { - $data = null; - - if (null !== $answer) { - $data = []; - /** @var CustomFormFieldAttribute $attribute */ - foreach ($answer->getAnswers() as $attribute) { - $type = $attribute->getCustomFormField()->getType(); - $name = $attribute->getCustomFormField()->getName(); - - switch ($type) { - case AbstractField::DATE_T: - case AbstractField::DATETIME_T: - $data[$name] = new \DateTime($attribute->getValue()); - break; - case AbstractField::BOOLEAN_T: - $data[$name] = (bool) $attribute->getValue(); - break; - case AbstractField::MULTIPLE_T: - case AbstractField::CHECK_GROUP_T: - $data[$name] = explode(static::ARRAY_SEPARATOR, $attribute->getValue()); - break; - default: - $data[$name] = $attribute->getValue(); - } - } - } - - return $this->formFactory->create(CustomFormsType::class, $data, array_merge($options, [ - 'customForm' => $this->customForm, - 'forceExpanded' => $forceExpanded, - ])); - } - /** * @param mixed $rawValue * @return string diff --git a/src/Form/Constraint/Recaptcha.php b/src/Form/Constraint/Recaptcha.php index f25da645..5affe036 100644 --- a/src/Form/Constraint/Recaptcha.php +++ b/src/Form/Constraint/Recaptcha.php @@ -11,8 +11,8 @@ class Recaptcha extends Constraint public string $emptyMessage = 'you_must_show_youre_not_robot'; public string $invalidMessage = 'recaptcha_is_invalid.try_again'; public string $fieldName = 'g-recaptcha-response'; - public ?string $privateKey = null; - public ?string $verifyUrl = null; + public string $privateKey = ''; + public string $verifyUrl = ''; /** * @return string[] diff --git a/src/Form/CustomFormsType.php b/src/Form/CustomFormsType.php index f6afde12..c0a6846b 100644 --- a/src/Form/CustomFormsType.php +++ b/src/Form/CustomFormsType.php @@ -23,7 +23,6 @@ use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\All; use Symfony\Component\Validator\Constraints\Email; @@ -69,8 +68,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) */ if ( !empty($options['recaptcha_public_key']) && - !empty($options['recaptcha_private_key']) && - !empty($options['request']) + !empty($options['recaptcha_private_key']) ) { $verifyUrl = !empty($options['recaptcha_verifyurl']) ? $options['recaptcha_verifyurl'] : @@ -82,7 +80,7 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'publicKey' => $options['recaptcha_public_key'], ], 'constraints' => [ - new Recaptcha($options['request'], [ + new Recaptcha([ 'privateKey' => $options['recaptcha_private_key'], 'verifyUrl' => $verifyUrl, 'fieldName' => $options['recaptcha_name'] @@ -301,7 +299,6 @@ public function configureOptions(OptionsResolver $resolver) 'recaptcha_private_key' => null, 'recaptcha_verifyurl' => null, 'recaptcha_name' => 'g-recaptcha-response', - 'request' => null, 'forceExpanded' => false, 'csrf_protection' => false, ]); @@ -310,7 +307,6 @@ public function configureOptions(OptionsResolver $resolver) $resolver->setAllowedTypes('customForm', [CustomForm::class]); $resolver->setAllowedTypes('forceExpanded', ['boolean']); - $resolver->setAllowedTypes('request', [Request::class, 'null']); $resolver->setAllowedTypes('recaptcha_public_key', ['string', 'null', 'boolean']); $resolver->setAllowedTypes('recaptcha_private_key', ['string', 'null', 'boolean']); $resolver->setAllowedTypes('recaptcha_verifyurl', ['string', 'null', 'boolean']); diff --git a/src/Mailer/ContactFormManager.php b/src/Mailer/ContactFormManager.php index 87c0d555..d7079710 100644 --- a/src/Mailer/ContactFormManager.php +++ b/src/Mailer/ContactFormManager.php @@ -296,7 +296,7 @@ public function withGoogleRecaptcha( 'publicKey' => $publicKey, ], 'constraints' => [ - new Recaptcha($this->getRequest(), [ + new Recaptcha([ 'fieldName' => $validatorFieldName, 'privateKey' => $privateKey, 'verifyUrl' => $verifyUrl,