1

I'm using a Webform, which have 3 fields:

- number of table (enter by user)

- price per table (default webform value)

- total amount to pay (calculation using formula, in webform is hidden field with no default value)

The calculation formula should be:
total amount to pay = number of table * price per table

What I want to do is add in total amount to pay after calculation and submit to webform.

I'm writing the codes as below but it does not work.

function my_module_validate($form, &$form_state) {
  if ($form_id == 'webform_client_form_7'){
        $form['submitted']['total']['#value'] = $form['submitted']['number_of_tables']['#webform_component']['value'] *  $form['submitted']['price_per_table']['#value'];
    }
}

Am I writing anything wrong?

1
  • Alter the values in $form_state['values'] instead of the $form. Commented Aug 25, 2014 at 7:37

1 Answer 1

2

List of issues with your code (random order):

  • You are taking $form by value, not by reference, so all changes you make to it are local, scoped to function body only. Make it my_module_validate(&$form, &$form_state) if you want to have a chance to alter form - but see next points, too.

  • You are passing values around using $form instead of $form_state - things that can change in the form's lifetime belongs to $form_state rather than $form (yes, simplification, but a good starting point). In specific case of Webform module that may be needed, but you are coding against expected practice, so you should expect some difficulties when doing so.

  • You are using validate hook for something that is not validation. You should rather prepend additional save handler for this purpose.

  • You calculate on input. Usual practice in Drupal is to save user data "as is", and do formatting and calculations at output. It allows greater flexibility and retroactive changes if algorithm change. Again, it's not set in stone, and your situation may need other approach. It's OK, as long as you remember that's not quite the way Drupal was designed to work.

Tobias Kirsch posted nice summary, and I fully agree with it:

What this means in your case: neither the validation nor the submission function of your form might be the right place for you to achive this. Maybe it would be better to add this calculation when the user is shown his submitted values. Honestly I think webform as a beginning might not be the right module to achieve this

3
  • 1
    What this means in your case: neither the validation nor the submission function of your form might be the right place for you to achive this. Maybe it would be better to add this calculation when the user is shown his submitted values. Honestly I think webform as a beginning might not be the right module to achieve this. Commented Aug 25, 2014 at 8:26
  • @TobiasKirsch if you wish to ad your explanation directly to my answer, I'll accept it right away. Or you may post your own answer (feel free to use pieces of mine). You deserve some credit for your explanation, and comments are ephemeral beings here.
    – Mołot
    Commented Aug 25, 2014 at 8:29
  • @Molot: As these are just some addtional notes from me added to your answer which already gave the main input I would not demand any credits. Thank you for your hints - I already realized it and did it that way with fully conscious hoping that you agree with these explanations. Commented Aug 25, 2014 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.