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
Since your form definition is
Since your form definition is named myFunction_form(), then your validation function would be:
And your submit function would be:
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.
Thanks Jaypan, I have
Thanks Jaypan, I have corrected my function names.
Just so I have this correct I now have 3 functions in
myCustomModule.admin.incIs this all looking correct? It's just you didn't have the word
functionbefore you function names which for me gave an error.Lastly, Is it correct to put the
return $form;at the end of the functioncybercampbell_form? or chould it be somewhere else?Thanks again.
C
Hi, Yes that's the correct
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 :
Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form
Thanks Sagar Ramgade
Thanks Sagar Ramgade
Is this all looking correct?
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:
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.
Hi Jaypan,
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_statereturn $form_state;}Nothing is returned from
Nothing is returned from validation or submit handlers.
Contact me to contract me for D7 -> D10/11 migrations.
Thanks Jaypan for reply.
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?
Changes to the form cannot be
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.
Alternate Form Validation/Submission function assignment
Hi, Will this also work? I am aware that this method is usually used in hook_form_alter.
http://japodomingo.com
Yes, something like that
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).
Adriadrop Drupal development