1

I have a form in my drupal project which has a managed_file field. It worked perfectly until I added invisible recaptcha for the forms. The file is getting uploaded but when I submit the form the windows gets refreshed with all the elements without doing anything.

Form Fields

$form['fileLoad'] = [
    '#type'              => 'managed_file',
    '#name'              => 'fileLoad',
    '#title'             => t(''),
    '#size'              => 20,
    '#description'       => t('Word/Excel/PDF/JPG/PNG formats supported'),
    '#upload_location'   => 's3://forms/contactus',
    '#upload_validators' => [
        'file_validate_extensions' => ['pdf doc docx xlsx jpg png'],
    ],
    '#attributes'        => [
        'id'    => 'fileLoad',
        'class' => ['form-control'],
    ],
    '#required'          => false,
];

$form['submit'] = [
    '#type'       => 'submit',
    '#value'      => t('submit'),
    '#attributes'   => [
        'id'            => 'validateBtn',
        'data-sitekey'  => "##MYSITEKEY##",
        'data-callback' => "onSubmit",
        'class'         => ['g-recaptcha', 'cus-btn-book-now'],
    ],
    '#validated'  => true,
];

Invisible recaptcha script

<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
   function onSubmit(token) {
       document.getElementById('contact_us_form').submit();
   }
</script>

The form works perfectly

  • With the managed_file field, without invisible recaptcha (works perfectly with recaptcha V2)
  • Without managed_file field with the invisible recaptcha

I guess the form doesn't work when both the invisible recaptcha and managed_file field present. What would be the solution?

  • 1
    I'm voting to close this question as off-topic because bugs must be reported on drupal.org – leymannx Jul 30 '18 at 6:24
  • I guess it's not actually a bug report because it looks like it is doing the recaptcha integration by hand. Look at the recaptcha module and then in-progress patch there for invisible recaptcha, afaik ajax integration is exactly one of the problems that they are trying to solve there. – Berdir Jul 30 '18 at 7:08
  • @Berdir I am not using any module for this. This is simply a callback to the google invisible recaptcha. But unfortunately without getting submitted, the form is getting reloaded – i am batman Jul 30 '18 at 7:24
  • Yes, that's what I assumed, but my point is you have to, because apparently, it does not work when the form uses ajax out of the box and the patch in the recaptcha module is trying to fix that, I don't know about the current status – Berdir Jul 30 '18 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.