-3

I have created a custom form in my drupal 8 project. I need the form data to be inserted into my mysql database. Can somebody tell me how? Below is the custom form php file

<?php

namespace Drupal\custom_form\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Select;
use Drupal\Core\Render\Element\Checkbox;

class CustomForm extends FormBase {

    public function getFormId() {
        return 'custom_form';
    }

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

        $form['name'] = array(
            '#type' => 'textfield',
            '#title' => t('Name'),
            '#required' => TRUE,
        );

        $form['address'] = array(
            '#type' => 'textarea',
            '#title' => t('Address'),
            '#required' => TRUE,
        );

        $form['gender'] = array(
            '#type' => 'radio',
            '#title' => t('Gender'),
            '#options' => [
                t('Male'),
                t('Female'),
                t('Other'),
            ],
            '#required' => TRUE,
        );  

        $form['nationality'] = array(
            '#type' => 'select',
            '#title' => t('Nationality'),
            '#options' => [
                'indian' => t('Indian'),
                'other' => t('Other'),
            ],
            '#required' => TRUE,
        );

        $form['languages'] = array(
            '#type' => 'select',
            '#title' => t('Languages Known'),
            '#multiple' => TRUE,
            '#options' => [
                'english' => t('English'),
                'kannada' => t('Kannada'),
            ]
        );

        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => $this->t('Save'),
            '#button_type' => 'primary',
        );

        return $form;
    }

    public function validateForm(array &$form, FormStateInterface $form_state) {
        if(!is_numeric($form_state->getValue('name'))) {
            $form_state->setErrorByName("Enter only Alphabets");
        }
    }

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

closed as too broad by kiamlaluno Feb 12 at 5:58

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to Drupal Answers! Drupal has classes to access the database, as described in Database abstract layer. We cannot list here all those classes, and explain when and how to use them, since we would just end up writing here what the documentation says. If you have a more specific question, that could be on-topic for us. Be sure to also check the questions tagged database. – kiamlaluno Feb 12 at 6:38
  • I figured it out. the below code worked. public function submitForm(array &$form, FormStateInterface $form_state) { $values = $form_state->getValues(); $conn = \Drupal::database(); $conn->insert('formcustom')->fields( array( 'name' => $values['name'], 'address' => $values['address'], 'gender' => $values['gender'], 'nationality' => $values['nationality'], ) )->execute(); } – Vishnu V Feb 12 at 10:26
-1

With the help of submitForm method You can submit your form. public function submitForm(array &$form, FormStateInterface $form_state,$user = NULL) { $node = Node::create(['type' => '']); $node->set('title', ''); $node->set('uid',1); $node->status = 1; $node->save(); }

  • how will this help in adding the data to a table in the database? Where do we specify the database name, table name and the query? – Vishnu V Feb 11 at 13:48

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