Reputation: 2606
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: myFunction.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.
Upvotes: 1
Views: 5229
Reputation: 512
You should follow the standard naming structure used by the form API. If you function is:
myFunction_form(),
then
myFunction_form_validate() will be called when submitted to perform any validation. If everything passes, then
myFunction_form_submit() will be called. You should place your submit logic (or a call to your custom function) in the submit.
This is the preferred behavior over setting $form['#submit'][] and $form['#validate'][].
See the Examples Module for simple examples of how this works.
Upvotes: 1
Reputation: 26
Approach it this way:
$form['#validate'][] = 'myCustomValidateFunction';
$form['#submit'][] = 'myCustomSubmitFunction';
function myCustomValidateFunction($form, &$form_state) {
\\if validation was not passed use form_set_error()
}
function myCustomSubmitFunction($form, &$form_state) {
//submit logic, $form_state includes the values
}
Upvotes: 0
Reputation: 420
Yes you propably go the wrong way. You will have to add your custom validator and submit functions like
<?php
$form['#submit'][] = my_submit_callback
$form['#validate'][] = my_validator_callback
function my_submit_callback($form, &$form_state) {
// form_state array contains the submitted values
}
function my_validator_callback($form, &$form_state) {
// form_state array contains the submitted values
if ($form_state['values']['body'] == '') {
form_set_error(...)
}
}
and ofcourse remove the myFunction() call from your form_builder function
Upvotes: 0