【问题标题】:Validating Botdetect recaptcha in Symfony 4 Without using an entity在 Symfony 4 中验证 Botdetect 验证码而不使用实体
【发布时间】:2019-02-20 08:50:15
【问题描述】:

有什么方法可以在 symfony 表单构建器中验证 BotDetect recaptcha 吗?

我有下面的表格,可以让用户输入他们的电子邮件。:

     $form = $this->createFormBuilder()
        ->add('email', EmailType::class,[
            'label' => false,
            'attr' => [
                'style' => 'text-align:center;',
                'value' => $email,
            ]
        ])
        ->add('captchaCode', CaptchaType::class, array(
            'captchaConfig' => 'ExampleCaptcha'
        ))
        ->add('Do some shiz wif my email bruh.', SubmitType::class)
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        return $this->redirect('/unblock/'.$email);
    }

问题在于,他们的文档在实体中指定了验证约束。我的应用程序还没有任何实体,但我想知道是否有人找到了从控制器验证验证码的方法?

我可以在创建实体时将其添加到实体中,但我想知道如何在没有任何实体或连接到数据库的应用程序上完成。

【问题讨论】:

    标签: php forms recaptcha symfony4 botdetect


    【解决方案1】:

    我最终使用了来自 packagist 的 beelabs google recaptcha 包(如果有人感兴趣),链接:https://packagist.org/packages/beelab/recaptcha2-bundle

    他们的文档几乎可以告诉您有关安装和设置的所有信息。

    一个缺点是它仍然允许表单提交,即使你没有点击我不是机器人复选框,所以你需要在 PHP 端验证它是否被点击。

    您可以使用它来获取响应,该响应通常是散列或空字段。

    $recaptcha = $request->get('g-recaptcha-response', '');
    

    【讨论】:

      【解决方案2】:
      use Captcha\Bundle\CaptchaBundle\Validator\Constraints\ValidCaptcha;
      
          $form = $this->createFormBuilder()
           ->add('captchaCode', CaptchaType::class, [
                          'captchaConfig' => 'LoginCaptcha',
                          'constraints' => [
                              new ValidCaptcha([
                                  'message' => 'CAPTCHA validation failed, try again.'
                              ])
                          ]
                      ]);
      

      【讨论】:

        猜你喜欢
        • 2018-12-07
        • 1970-01-01
        • 2019-10-05
        • 1970-01-01
        • 2019-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多