Skip to content

Commit

Permalink
fix: Removed request option from Recaptcha constraint and using Form …
Browse files Browse the repository at this point in the history
…classes
  • Loading branch information
ambroisemaupate committed May 19, 2022
1 parent 4121345 commit 60a04d4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 57 deletions.
48 changes: 0 additions & 48 deletions src/CustomForm/CustomFormHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
]);
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Constraint/Recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
8 changes: 2 additions & 6 deletions src/Form/CustomFormsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'] :
Expand All @@ -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']
Expand Down Expand Up @@ -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,
]);
Expand All @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion src/Mailer/ContactFormManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function withGoogleRecaptcha(
'publicKey' => $publicKey,
],
'constraints' => [
new Recaptcha($this->getRequest(), [
new Recaptcha([
'fieldName' => $validatorFieldName,
'privateKey' => $privateKey,
'verifyUrl' => $verifyUrl,
Expand Down

0 comments on commit 60a04d4

Please sign in to comment.