_submit and _validate get the form in a ordinate way... but they don't output anything on screen.
Suppose I want to do something as stupid as multiplying 2 fields and return the output on screen without having to save the 2 fields somewhere... as if _submit was not going to return a path but just use $form_values in spite of $_POST and output something...
I gave a look at how search works and at a quick glance it looks not suited. Up to my understanding it parse keys and then _submit append it to a url.
thanks
--- Yes this is correct. Submit and validate fire before any form rendering is done. It's not perfect but it's what is there. Part of the rational for doing this is that if you return path from a submit handler drupal will redirect to that page.
If you want to multiply the contents of two fields you are going to do it like this:
1. IN the submit handler modify the data that your application is based on, using session variables or some other permanent data store (the database is most common) and alter data.
2. Reference these structures in the form rendering function to display the data.
function test_form() { $form['value1'] = array('#type' => 'textfield', '#title' => 'Value 1', '#default_value' => '3', );
$form['value2'] = array('#type' => 'textfield', '#title' => 'Value 1', '#default_value' => '3', );
$form['total'] = array('#type' => 'item', '#title' => 'Total', '#value' => $_SESSION['mymodule']['total'], ); $form['submit'] = array( '#type' => 'submit', '#value' => 'multiply', ); return $form; }
function test_form_submit($formid, $form_values) { $_SESSION['mymodule']['total'] = $form_values['value1'] * $form_values['value2'];
}
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Ivan Sergio Borgonovo Sent: Wednesday, February 06, 2008 5:15 AM To: support@drupal.org Subject: [support] FAPI and "returned" page
_submit and _validate get the form in a ordinate way... but they don't output anything on screen.
Suppose I want to do something as stupid as multiplying 2 fields and return the output on screen without having to save the 2 fields somewhere... as if _submit was not going to return a path but just use $form_values in spite of $_POST and output something...
I gave a look at how search works and at a quick glance it looks not suited. Up to my understanding it parse keys and then _submit append it to a url.
thanks
On Wed, 6 Feb 2008 08:26:37 -0800 "Metzler, David" metzlerd@evergreen.edu wrote:
Yes this is correct. Submit and validate fire before any form rendering is done. It's not perfect but it's what is there. Part of the rational for doing this is that if you return path from a submit handler drupal will redirect to that page.
If you want to multiply the contents of two fields you are going to do it like this:
- IN the submit handler modify the data that your application is
based on, using session variables or some other permanent data store (the database is most common) and alter data.
- Reference these structures in the form rendering function to
display the data.
[snip]
function test_form_submit($formid, $form_values) {
$_SESSION['mymodule']['total'] = $form_values['value1'] * $form_values['value2'];
}
OK... at least I know there aren't smarter routes. A bit painful if you've large forms.
Nothing forbid to catch the $_POST since it is conserved.
Part of FAPI still remain useful, but _validate becomes cosmetic. It'd be nice to have a _display hook that just takes the same arguments as _submit and if present directly show some output in spite of returning drupal_render_form...
I think something can be recycled out of FAPI for my scenario... but it won't be as elegant as if we had a _display hook.
thanks
Yes, remember also the ('#type'=>'hidden') they're good for passing data from the form.... just like making hidden fields in html. These are all passed to the $form_values section of the form array.
I also wanted to make sure you know using form_set_error to indicate what fields contain errors etc.
It took me a while to find this the first time.
Happy coding :)
Dave
-----Original Message----- From: support-bounces@drupal.org [mailto:support-bounces@drupal.org] On Behalf Of Ivan Sergio Borgonovo Sent: Wednesday, February 06, 2008 9:56 AM To: support@drupal.org Subject: Re: [support] FAPI and "returned" page
On Wed, 6 Feb 2008 08:26:37 -0800 "Metzler, David" metzlerd@evergreen.edu wrote:
Yes this is correct. Submit and validate fire before any form rendering is done. It's not perfect but it's what is there. Part of the rational for doing this is that if you return path from a submit handler drupal will redirect to that page.
If you want to multiply the contents of two fields you are going to do it like this:
- IN the submit handler modify the data that your application is
based on, using session variables or some other permanent data store (the database is most common) and alter data.
- Reference these structures in the form rendering function to
display the data.
[snip]
function test_form_submit($formid, $form_values) {
$_SESSION['mymodule']['total'] = $form_values['value1'] * $form_values['value2'];
}
OK... at least I know there aren't smarter routes. A bit painful if you've large forms.
Nothing forbid to catch the $_POST since it is conserved.
Part of FAPI still remain useful, but _validate becomes cosmetic. It'd be nice to have a _display hook that just takes the same arguments as _submit and if present directly show some output in spite of returning drupal_render_form...
I think something can be recycled out of FAPI for my scenario... but it won't be as elegant as if we had a _display hook.
thanks
On Wed, 6 Feb 2008 14:15:27 +0100 Ivan Sergio Borgonovo mail@webthatworks.it wrote:
_submit and _validate get the form in a ordinate way... but they don't output anything on screen.
Suppose I want to do something as stupid as multiplying 2 fields and return the output on screen without having to save the 2 fields somewhere... as if _submit was not going to return a path but just use $form_values in spite of $_POST and output something...
I gave a look at how search works and at a quick glance it looks not suited. Up to my understanding it parse keys and then _submit append it to a url.
I tried to play a bit with multistep forms since it looks as the only system to have $form_values back into the form generating function.
So the form function change it's output according to the received $form_values. I know _validate get called, but it seems that form_set_values() inside _validate has no effect on the values received by the form generating function.
Not that I really need to use form_set_value... but just for better understanding of how things work...
Another thing I don't need now but that is out of my understanding is how to succeed in getting the $form_values in the form generating function *and* execute the _submit hook.
If the form is multistep, it doesn't fire the _submit hook so I'd be curious to know how you could have the $form_values *and* fire the _submit without the need to load what you submitted from the DB/session/whatever.
thx