0

I created my form but inserting the base does not work also radios type does not appear in the form

<?php

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

namespace Drupal\contact_form\Form;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ChangedCommand;
use Drupal\Core\Ajax\CssCommand;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Ajax\InvokeCommand;

/**
 * Contribute form.
 */

class ContributeForm extends FormBase implements FormInterface{
    protected $accepted_domains = ['gmail.com', 'yahoo.com'];

    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'contact_form_contribute_form';
    }

    /**
     * {@inheritdoc}
     */

    public function buildForm(array $form, FormStateInterface $form_state) {

        $form['FullName'] = array(
            '#type' => 'textfield',
            '#title' => $this->t('Full Name'),
            '#required' => TRUE,

        );
        $form['Email'] = array(
            '#type' => 'email',
            '#title' => $this->t('Email address'),
            '#required' => true,
            '#size' => 60,
            '#maxlength' => 128,

        );

        $form['Feedback'] = array(
            '#type' => 'radios',
            '#title' => $this->t('Feedback'),
            '#description' => t('Type of feedback'),
            '#default_value' => 0,
            '#options' => array(
                0 =>'Comments or suggestions',
                1 => 'Questions',
                2 =>'Report a problem(s)',
                3 =>'Other',
            )
        );
        $form['Subject'] = array(
            '#type' => 'textfield',
            '#title' => $this->t('Subject'),
            '#required' => true,
        );
        $form['Message'] = array(
            '#type' => 'textarea',
            '#title' => $this->t('Your Message'),
            '#required' => true,
            '#cols' => 90,
            '#rows' => 10,
        );

        $form['captcha'] = array(
            '#type' => 'captcha',
            '#captcha_type' => 'recaptcha/reCAPTCHA',
            '#title' => $this->t('Captcha:'),
            '#required' => true,
        );

        $form['save'] = array(
            '#type' => 'submit',
            '#input' => TRUE,
            '#value' => 'save',
            '#submit' => array('buildForm')
        );


        return array(
            '#form' => $form,
            '#theme' => 'contribute',


        );

    }

    /**
     * {@inheritdoc}
     */

    public function validateForm(array &$form, FormStateInterface $form_state) {
    }

    /**
     * {@inheritdoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        $name = $form_state->getValue('FullName');
        $email = $form_state->getValue('Email');
        $subject = $form_state->getValue('Subject');
        $type = $form_state->getValue('TypeFeedback');
        $msg = $form_state->getValue('Message');
        $field = array(

            'FullName' =>  $name,
            'Email' => $email,
            'TypeFeedback' =>  $type ,
            'Subject' => $subject,
            'Message' =>  $msg,
            'Etat_message'=>  '',
            'Date_contact'=>  '',
            'adresse_IP'=>  '',
        );
        db_insert('contact')
            ->fields($field)
            ->execute();
        drupal_set_message("succesfully saved");


    }
}

enter image description here

  • I think you need to create a services to store in DB. – Suraj Apr 10 '17 at 11:21
1

Removing the #submit key fixed the submission issue for me:

$form['save'] = array(
    '#type' => 'submit',
    '#input' => TRUE,
    '#value' => 'save',
    //'#submit' => array('buildForm')
);

DB insert works correctly.

If you do return $form; in the buildForm() method, the radios form element is rendered correctly. Looks like your theme might be causing the issue.

| improve this answer | |
  • You can't return the form as #something. It must be a real render array, otherwise it doesn't get parsed. If you want a theme function, use #theme on the render array. – Berdir Apr 12 '16 at 21:18
  • I understood nothing of what the solution – Sony Apr 12 '16 at 21:26
  • @sony, in your public function buildForm(array $form, FormStateInterface $form_state) replace the form['save'] array with the snippet above. And replace your return array('#form' => $form, '#theme' => 'contribute'); with return $form;. That will fix the issue. – mkudenko Apr 12 '16 at 21:35
  • @mkudenko, how to do it exactly? – Sony Apr 12 '16 at 21:36
  • I do this because I have a theme to a form theming (contribute.html.twig) – Sony Apr 12 '16 at 21:39

Your Answer

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

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