Hi,

I've create a module with a custom form in the configuration panel.
Now in my form there are some fields that need to be saved in my DB (created when activating my module). I've searched for a hook that should save the form, but didn't find anything usefull yet.

This is my code for showing the form:

function fb_recommend_form($form, &$form_state){
	$form = array();
	
	$form['url'] = array(
		'#type' => 'textfield',
		'#title' => 'Url to recommend',
		'#description' => t('Enter the url of the site that you want to use for the Facebook Recommnendation box'),
		'#size' => 50,
		'#maxlength' => 255,
		'#required' => TRUE
	);
	
	$form['height'] = array(
	 	'#type' => 'textfield',
	 	'#title' => t('The height of the Facebook Recommendation box'),
	 	'#description' => t('Enter the desired height (without pixels or px) of the Facebook Recommendation box'),
	 	'#size' => 10,
	 	'#maxlength' => 3,
	 	'#required' => FALSE
	);
	
	$form['width'] = array(
	 	'#type' => 'textfield',
	 	'#title' => t('The width of the Facebook Recommendation box'),
	 	'#description' => t('Enter the desired width (without pixels or px) of the Facebook Recommendation box'),
	 	'#size' => 10,
	 	'#maxlength' => 3,
	 	'#required' => FALSE
	);

	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Save settings'),
	);
	
	return $form;
}

Anyone knows which hook I can use to save this form?

Thanks!

Comments

Jaypan’s picture

You use the form definition function name and add the suffix _submit.

Your form definition function name is: fb_recommend_form()

So your submit function would be: fb_recommend_form_submit()

You can also have a validation function: fb_recommend_form_validate()

Yaeko-1’s picture

Ok cool, I found it thanks, but I'm getting an error when saving:

function fb_recommend_form_submit($form, $form_state){
	$values = array(
		'url' => $form_state['values']['url'],
		'width' => $form_state['values']['width'],
		'height' => $form_state['values']['height']   
	);
	
	$insert = db_insert('fb_recommend')
		-> fields(array(
			'url' => $values['url'],
			'width' => $values['width'],
			'height' => $values['height'],
		))
		->execute();
	
	drupal_set_message(t('Settings have been saved'));
}

error:

PDOException: SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'width' at row 1: INSERT INTO {fb_recommend} (url, width, height) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => http://patlezizmo.be [:db_insert_placeholder_1] => 200 [:db_insert_placeholder_2] => 300 ) in fb_recommend_form_submit() (line 130 of /Users/****/Sites/drupal-7/sites/all/modules/custom/fb_recommend/fb_recommend.module).

Not sure if it has to do with my column properties?

Yaeko-1’s picture

Never mind, found it :)

meeta’s picture

tice: Undefined index: outscript in helloworld_form_submit() (line 55 of D:\xampp\htdocs\helloworld\sites\all\modules\helloworld\helloworld.module).
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'outscript' cannot be null: INSERT INTO {helloworld} (serverurl, inscript, outscript) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 2333 [:db_insert_placeholder_1] => dfsf [:db_insert_placeholder_2] => ) in helloworld_form_submit() (line 64 of D:\xampp\htdocs\helloworld\sites\all\modules\helloworld\helloworld.module).

meeta’s picture

otice: Undefined index: outscript in helloworld_form_submit() (line 55 of D:\xampp\htdocs\helloworld\sites\all\modules\helloworld\helloworld.module).
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'outscript' cannot be null: INSERT INTO {helloworld} (serverurl, inscript, outscript) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 2333 [:db_insert_placeholder_1] => dfsf [:db_insert_placeholder_2] => ) in helloworld_form_submit() (line 64 of D:\xampp\htdocs\helloworld\sites\all\modules\helloworld\helloworld.module).

Jaypan’s picture

He didn't have that error.

krishanchandra’s picture

This is worked for me.........

  public function buildForm(array $form, FormStateInterface $form_state) {
	$form['title'] = array(<?php
      '#type' => 'textfield',
      '#title' => t('Title'),
      '#required' => TRUE,
    );
    $form['video'] = array(
      '#type' => 'textfield',
      '#title' => t('Youtube video'),
    );
    $form['description'] = array(
      '#type' => 'textarea',
      '#title' => t('Description'),
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
	// Validate video URL.
    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) {
  $values = array(
		'title' => $form_state->getValue('title'),
		'video' => $form_state->getValue('video'),
		'description' => $form_state->getValue('description'),
		
	);
	$insert = db_insert('amazing_forms')
		-> fields(array(
			'title' => $values['title'],
			'video' => $values['video'],
			'description' => $values['description'],
		))
		->execute();
	
	drupal_set_message(t('Settings have been saved'));
  }
sksankarraj’s picture

amazing_forms <- do i need to create this table?

bardiuk’s picture

Is this approach still valid in 2022? To write directly into DB?