You are here

function commerce_billy_admin_form_validate in Commerce Billy 7

Validation handler for Commerce Billy admin form.

File

./commerce_billy.admin.inc, line 91
Settings for Commerce Billy.

Code

function commerce_billy_admin_form_validate($form, &$form_state) {

  // If we on confirm form just skip validation.
  if (!empty($form_state['values']['nr_override'])) {
    return;
  }
  $pattern = $form_state['values']['commerce_billy_invoice_nr_pattern'];
  if (strpos($pattern, '{id}') === FALSE) {
    form_set_error('commerce_billy_invoice_nr_pattern', t('Invalid pattern. {id} is missing.'));
  }
  elseif ($pattern === '{id}') {
    form_set_error('commerce_billy_invoice_nr_pattern', t('You must include additional characters to {id}, as otherwise the invoice number might overlap with the order ID, leading the database exceptions.'));
  }

  // Check invoice id padding input.
  if (preg_match('#\\D#', $form_state['values']['commerce_billy_invoice_nr_padding']) || $form_state['values']['commerce_billy_invoice_nr_padding'] < 0 || $form_state['values']['commerce_billy_invoice_nr_padding'] > 20) {
    form_set_error('commerce_billy_invoice_nr_padding', t('Please enter a number from 0 to 20 for invoice id padding.'));
  }

  // Check for valid characters.
  $test_nr = str_replace('{id}', 1, $pattern);
  $test_nr = token_replace($test_nr, array(), array(
    'clear' => TRUE,
  ));
  if (!commerce_order_validate_number_characters($test_nr)) {
    form_set_error('commerce_billy_invoice_nr_pattern', t('Invalid pattern. Only alphanumeric values, underscores and dashes are allowed.'));
  }
}