0

I am trying to show a CAPTCHA image in the form of my custom form, but it doesn't appear.

enter image description here

Why doesn't this code show the CAPTCHA as expected?

/**
 * Created by PhpStorm.
 * User: sony
 * Date: 28/02/2016
 * Time: 23:23
 */

/**
 * @file
 * Contains \Drupal\contact_form\Form\ContributeForm.
 */

namespace Drupal\contact_form\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;

/**
 * Contribute form.
 */
class ContributeForm extends FormBase {
    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'contact_form_contribute_form';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {
        $form['Full Name'] = array(
            '#type' => 'textfield',
            '#title' => t('Full Name'),
            '#required' => TRUE,
        );
        $form['E-mail'] = array(
            '#type' => 'email',
            '#title' => t('E-mail'),
        );

        $form['Type of feedback'] = array(
            '#type' => 'radios',
            '#title' => t('Feedback'),
            '#description' => t('Type of feedback'),
            '#options' => array(
                t('Comments or suggestions'),
                t('Questions'),
                t('Report a problem(s)'),
                t('Other'),
            )
        );
        $form['Subject'] = array(
            '#type' => 'textfield',
            '#title' => t('Subject'),
        );
        $form['Your Message'] = array(
            '#type' => 'textarea',
            '#title' => t('Your Message'),
        );
        $form['my_captcha_element'] = array(
            '#type' => 'captcha',
            '#captcha_type' => 'Image',
        );

        $form['sendt'] = array(
            '#type' => 'submit',
            '#value' => t('Send'),
        );
        return $form;

    }

    /**
     * {@inheritdoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state) {
        if (!UrlHelper::isValid($form_state->getValue('video'), TRUE)) {
            $form_state->setErrorByName('video', $this->t("The video url '%url' is invalid.", array('%url' => $form_state->getValue('video'))));
        }
    }

    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        foreach ($form_state->getValues() as $key => $value) {
            drupal_set_message($key . ': ' . $value);
        }
    }

2 Answers 2

1

Captcha is not a valid form element in Drupal 8. Since you haven't listed if you are using any other contributed modules, I am not sure what or where #type => captcha comes from.

I advise using Mollom to do captcha handling for forms.

https://www.drupal.org/project/mollom

2
  • how to use this module?
    – Sony
    Commented Mar 1, 2016 at 21:37
  • Check the module README. It will protect forms by form id.
    – Kevin
    Commented Mar 2, 2016 at 14:28
0

Captcha is a valid form element if you have captcha module installed.

$form['tabs']['upload']['captcha'] = [
  '#type' => 'captcha',
  '#captcha_type' => 'captcha/Math',
]

if you want to use recaptcha, the type is '#captcha_type' => 'recaptcha/reCAPTCHA',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.