I modified the module to allow for a field to have multiple options and be summed. I tested this with checkboxes, select and multiselects and was able to get the desired result.

Previously, I would get a validation error that said "(component_name) amount is invalid" because it was not expecting an array.

Here is my change. Bear with me, not sure how to create a patch in git.


function _webform_pay_component_value($node, $component, $values) {
  $parent_keys = webform_component_parent_keys($node, $component);
  foreach ($parent_keys as $form_key) {
    if (isset($values[$form_key]) && !is_array($values[$form_key])) {
      $values = $values[$form_key];
    } elseif (isset($values[$form_key]) && is_array($values[$form_key])) {
      foreach ($values[$form_key] as $field_amount => $field_label) {
        if (is_numeric($field_amount)) {
          $total += $field_amount;
        }
      }
      $values = $total;
    } else {
      $values = FALSE;
      break;
    }
  }
  return $values;
}

Comments

kevinquillen’s picture

StatusFileSize
new871 bytes

Attaching my first ever git patch...

kevinquillen’s picture

Title: Validate amount if pay component is checkbox, select, or multiselect » Validate amount if pay component is checkbox, or multiselect
kevinquillen’s picture

 function _webform_pay_component_value($node, $component, $values) {
  $parent_keys = webform_component_parent_keys($node, $component);
  foreach ($parent_keys as $form_key) {
    if (isset($values[$form_key])) {
      if (is_array($values[$form_key])) {
      	foreach ($values[$form_key] as $field_amount => $field_label) {
      		if (is_numeric($field_amount)) {
      			$total += $field_amount;
      		}
      	}
      	$values = $total;
      } else {
      	$values = $values[$form_key];
      }
    } else {
      $values = FALSE;
      break;
    }
  }
  return $values;
}

Rerolling..

kevinquillen’s picture

 function _webform_pay_component_value($node, $component, $values) {
  $parent_keys = webform_component_parent_keys($node, $component);
  foreach ($parent_keys as $form_key) {
    if (isset($values[$form_key])) {
      if (is_array($values[$form_key])) {
      	foreach ($values[$form_key] as $field_amount => $field_label) {
      		if (is_numeric($field_amount)) {
      			$total += $field_amount;
      		}
      	}
      	$values = $total;
      } else {
      	$values = $values[$form_key];
      }
    } else {
      $values = FALSE;
      break;
    }
  }
  return $values;
}

Rerolling..

kevinquillen’s picture

StatusFileSize
new819 bytes

Sorry for the dupes. It seems when I use the Git Instructions on the module I receive an old version of Webform Pay. Getting the hang of it now I think.

kevinquillen’s picture

StatusFileSize
new819 bytes

Previous upload goes to Page Not Found..

kevinquillen’s picture

StatusFileSize
new844 bytes

Had to add != FALSE after is_numeric check... looks like if you don't pick any options of a checkbox field, it passes the array of allowed values but changes the label to FALSE for each one.

Is that expected behavior of webform? Just wondering if it may have been something I did instead.

quicksketch’s picture

Title: Validate amount if pay component is checkbox, or multiselect » Checkboxes and multiple-select fields not validated or added correctly
Status: Active » Fixed
StatusFileSize
new1.74 KB

Thanks Kevin, I modified your patch to make it so that the _webform_pay_component_value() function is unaffected, since that function is also used on things that aren't the "price" totals at all. Otherwise we might do strange things like accidentally adding up a users address fields together. This patch adds support for checkboxes/multi-select fields and adds up the totals properly.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.