You are here

function uc_varprice_feature_form_validate in UC Variable Price 6

2 string references to 'uc_varprice_feature_form_validate'
uc_varprice_form_alter in ./uc_varprice.module
Implementation of hook_form_alter().
_uc_varprice_feature_form in ./uc_varprice.module

File

./uc_varprice.module, line 468
Defines a product feature to turn any product into a variable priced product.

Code

function uc_varprice_feature_form_validate($form, &$form_state) {

  // Check for invalid text in the "Selectable prices" field
  $form_state['values']['sel_options_arr'] = array();
  foreach (array_unique(explode("\n", $form_state['values']['sel_options'])) as $value) {
    list($value, $display) = explode('|', $value);
    $display = trim($display);

    // Be forgiving of spaces and blank lines
    $value = trim($value);
    list($value, $uniqueness) = explode(':', $value);
    if ($value !== '') {
      if (!is_numeric($value)) {
        form_set_error('sel_options', t('The value %val does not appear to be a number. Please enter only one number per line, without currency symbols.', array(
          '%val' => $value,
        )));
      }
      else {

        // We want the keys of this array to be in the same format as the
        // price_default field in the database (2 decimal points) so that we can
        // properly select the default on the select widget.
        // But we also want to be able to have multiple display options for the same value, so
        // you can have an optional unique identifier after the value, preceded by a colon.
        $value_key = $uniqueness ? sprintf('%.2f', $value) . ":{$uniqueness}" : sprintf('%.2f', $value);
        $form_state['values']['sel_options_arr'][$value_key] = $display ? $display : uc_currency_format($value);
      }
    }
  }
}