I have created a custom module that I need to collect info and run a function when a form is filled out and submitted.

It has two text areas and a button.

This is what I have which shows up fine on the page:

File: myCustomModule.admin.inc

    function myFunction_form($form)
    {
    	
    	$form['pages'] = array(
        '#type' => 'fieldset',
        '#title' => t('Data'),
        '#collapsible' => FALSE,
    	'#collapsed' => FALSE,
      );
      
        $form['pages']['title'] = array(
        '#type'          => 'textarea',
        '#title'         => t('Title'),
    	'#rows'			 => 5,
    	'#resizable'	=> FALSE,
      );
      
      $form['pages']['body'] = array(
        '#type'          => 'text_format',
        '#title'         => t('Body'),
    	'#rows'			 => 5,
    	'#resizable'	=> FALSE,
    	'#format' => 'full_html',
      );
      
      $form['submit'] = array('#type' => 'submit', '#value' => t('Run Function'));
    	
    	myFunction($form);
    	return $form;
    	
    }
    
    function myFunction()
    {
    //This is where I use the data collected from my form and do what I need to do.
    }

So the things I missing form this (and please tell me if i'm going the wrong way about this) is I need to validate the form was filled out and return error message if not.

if the form was filled out then correctly pass the field data to my function which I did simply by adding function myFunction() before return $form; but this seems like the wrong way to do it. I don't want the myFunction() to run if there is and errors with the form.

Could someone please help me with this last part of my custom module.

Please not that this module does NOT add anythig to the database.

Again, please tell me if i'm going the wrong way about this.

Comments

jaypan’s picture

Since your form definition is named myFunction_form(), then your validation function would be:

myFunction_form_validate($form, &$form_state)
{

}

And your submit function would be:

myFunction_form_submit($form, &$form_state)
{

}

As a side note, you should choose either camel-case, or using underscores, but not a mixture of both.

Contact me to contract me for D7 -> D10/11 migrations.

cybercampbell’s picture

Thanks Jaypan, I have corrected my function names.

Just so I have this correct I now have 3 functions in myCustomModule.admin.inc

function cybercampbell_form($form)
{
 // Builds Form.
return $form;
}
function cybercampbell_form_validate($form, &$form_state)
{
 // Validates Form fields.
}
function cybercampbell_form_submit($form, &$form_state)
{
 // Does something with successfully submited form fields.
}

Is this all looking correct? It's just you didn't have the word function before you function names which for me gave an error.

Lastly, Is it correct to put the return $form; at the end of the function cybercampbell_form? or chould it be somewhere else?

Thanks again.

C

sagar ramgade’s picture

Hi,

Yes that's the correct way to return the form array, in you form generation function you also need to mention form_state as the second argument like below :

function cybercampbell_form($form, &$form_state) {
// Builds Form.
return $form;
}

Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form

cybercampbell’s picture

Thanks Sagar Ramgade

jaypan’s picture

Is this all looking correct? It's just you didn't have the word function before you function names which for me gave an error.

Apologies, a typo on my part. I started by writing the function names, then decided to turn it into a piece of code. I missed that part in the crossover.

Anyways, your function names are correct. Your values will be stored in $form_state['values'], according to the key names you have given in your form definition. Note that you cannot directly make changes to the $form_state[ 'values'] element in your validation, and have those changes persist through to the submission function. If you are in this position, you can change the values in your $form_state using form_set_value().

Finally, you should ensure that your .inc file is loaded on every page load, by doing the following:

function cybercampbell_form($form, &$form_state)
{
// include .inc file
form_load_include($form_state, 'inc', 'my_module_name', 'myCustomModule.admin');

// Builds Form.
return $form;
}

This will ensure that the file is loaded on every page load. You would think it would have to be, but you should never count on that with included files as it all depends on how the page is loaded. This ensures consistency no matter how this form is loaded.

Contact me to contract me for D7 -> D10/11 migrations.

dks786’s picture

Hi Jaypan,

If we do something on $form_state or $form in these (validate or submit) handlers

myFunction_form_validate($form, &$form_state){}

OR

myFunction_form_submit($form, &$form_state){}

than do we need to return the $form_state or $form from that handler function?

like that:

myFunction_form_validate($form, &$form_state){

//do some code or alter in $form_state

return $form_state;

}

jaypan’s picture

Nothing is returned from validation or submit handlers.

Contact me to contract me for D7 -> D10/11 migrations.

dks786’s picture

Thanks Jaypan for reply.

But if we need to make changes in $form ($form does not contain '&' like &$form_state), than also it will work?

& what will happen if we return from here?
 

jaypan’s picture

Changes to the form cannot be made in the validation or submit handlers. Changes need to be made in the form definition. 

You can return something from them if you want, but nothing will happen. The return value  is ignored. 

Contact me to contract me for D7 -> D10/11 migrations.

japo32’s picture

Hi, Will this also work? I am aware that this method is usually used in hook_form_alter.

<?php
  function my_custom_form($form)
  {
    $form['pages'] = array(
      '#type' => 'fieldset',
      '#title' => t('Data'),
      '#collapsible' => FALSE,
      '#collapsed' => FALSE,
    );
  
    $form['pages']['title'] = array(
      '#type'          => 'textarea',
      '#title'         => t('Title'),
      '#rows'             => 5,
      '#resizable'    => FALSE,
    );
  
    $form['pages']['body'] = array(
      '#type'          => 'text_format',
      '#title'         => t('Body'),
      '#rows'             => 5,
      '#resizable'    => FALSE,
      '#format' => 'full_html',
    );
  
    $form['submit'] = array('#type' => 'submit', '#value' => t('Run Function'));
    $form['#validate'][] = 'my_custom_validate_function';
    $form['#submit'][] = 'my_custom_submit_function';
    return $form;
  }

  function my_custom_validate_function($form, &$form_state)
  {
    // validate here
  }

  function my_custom_submit_function($form, &$form_state)
  {
    // submit here
  }
?>
Marko B’s picture

Yes, something like that should work. You could put any function there. Doesnt even have to be named like the module it can be _whatever_ever_submit (should end with submit though).