1

I have a problem with my module that only creates a form to submit a validation. The target of this module is to set a variable that I put in a textfield. The problem is that it doesn't save the variable, it only shows the default value. (I use variable_set and variable_get)

This variable is necessary because I use this form in another module that get this variable from the database. (My variable is id_event)

This is my code of my own module:

<?php
function example_form_form1(){
    $form['id_event'] = array(
     '#type' => 'textfield',
     '#title' => t('Id Event'),
     '#required' => TRUE,
         '#value' => variable_get('id_event', 7), //Here is my dude
    );

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

function example_form_submit($form, $form_state){
    $id_event = $form_state['values']['id_event'];
        variable_set('id_event', $id_event);//here is my dude
        drupal_set_message($id_event);
}
?>

Now, this variable is necesary for other module that only get.

<?php
$file_json = $this->options['xml_file'].variable_get('id_event', 1);
//Here don't get the value that i put, only put the value for default.
?>

Thanks :D

  • Welcome to Drupal Answers. In the first sentence you say 'submit a validation'. Did you mean 'submit a value'? If so please edit the question. – Ashlar Jan 24 '12 at 17:58
3

There are a few problems I see.

  1. The submit function in this example doesn't follow the same naming
    convention as your form constructor.
  2. You are also setting the #value of the id_event form element when you probably want to be setting the #default_value (a #value cannot be changed by the user).
  3. Your functions are missing their default arguments, which doesn't necessarily mean they will break but is bar form.

Try this:

<?php

function example_form_form1($form, &$form_state) {
  $form['id_event'] = array(
    '#type' => 'textfield',
    '#title' => t('Id Event'),
    '#required' => TRUE,
    '#default_value' => variable_get('id_event', 7),
  );

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

function example_form_form1_submit(&$form, &$form_state) {
  variable_set('id_event', $form_state['values']['id_event']);
  drupal_set_message($id_event);
}
?>
|improve this answer|||||
3

The reason your current code isn't working is because your submit handler doesn't follow the correct naming convention (i.e. FORM_FUNCTION_NAME_submit) and you don't explicitly attach the submit handler to the form anywhere so it's not being run.

You can solve this by simply changing the nameof your submit handler:

function example_form_form1_submit($form, $form_state){

Or you could look at system_settings_form() which automates much of this process for you.

|improve this answer|||||
  • 1
    +1 for pointing out system_settings_form(). Always better to explain to people why things work the way they do. – Charlie Schliesser Jan 25 '12 at 1:59

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.