Drupal Commerce: How to validate payment method settings? The code add settings for commerce_mymodule (for example) commerce payment module. Can I validate 'mymodule_deadline' variable and how?

/**
 * Payment method callback: settings form.
 */
function commerce_mymodule_settings_form($settings = NULL) {
  $settings = (array) $settings + array(
    'mymodule_min' =&gt; '',
    'mymodule_secret' =&gt; '',
    'site_mode' =&gt; 'DEMO_MODE'
  );

  $form = array();

  $form['mymodule_deadline'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Order payment deadline'),
    '#description' =&gt; t('Deadline for payment - default 7 days (max. 30 days).'),
    '#default_value' =&gt; $settings['mymodule_deadline'],
  );

  return $form;
}
I found a solution - because it is a FORM I using the FORMAPI. More accurate hook_form_alter() to add my custom validation handler and next is trivial. Code:

/**
 * Implements hook_form_alter()
 */
function commerce_mymodule_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if ($form_id == 'rules_ui_edit_element') {
    if ($form_state['element_settings']['payment_method']['method_id'] == 'commerce_mypaymentmethod') {
      $form['#validate'][] = 'commerce_mypaymentmethod_settings_validate';
    }
  }
}


and validation function

function commerce_mymodule_settings_validate(&amp;$form, &amp;$form_state) {  
  $deadline = (int) $form_state['values']['parameter']['payment_method']['settings']['payment_method']['settings']['mymodule_deadline'];

  if ($deadline &lt; 1 || $deadline &gt; 30) {
    form_set_error('', t('Enter only digit positive value between 1 -30 (days) in Order payment deadline. @value', array('@value' =&gt; $deadline)));
  }
}