I am trying to show a CAPTCHA image in the form of my custom form, but it doesn't appear.
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);
}
}