You are here

function role_expire_user_admin_role_validate in Role Expire 2.x

Same name and namespace in other branches
  1. 8 role_expire.module \role_expire_user_admin_role_validate()
  2. 6 role_expire.module \role_expire_user_admin_role_validate()
  3. 7 role_expire.module \role_expire_user_admin_role_validate()

Form validation handler invoked by role_expire_form_user_admin_role_alter.

Ensure that the specified duration is a valid, relative, positive strtotime- compatible string.

1 string reference to 'role_expire_user_admin_role_validate'
role_expire_form_user_role_form_alter in ./role_expire.module
Implements hook_form_FORM_ID_alter().

File

./role_expire.module, line 70
Role Expire module.

Code

function role_expire_user_admin_role_validate($form, FormStateInterface &$form_state) {
  $values = $form_state
    ->getValues();
  if (!empty($values['role_expire'])) {
    $duration_string = Html::escape($values['role_expire']);

    /*
     * Make sure it's a *relative* duration string. That is, it will result in a
     * different strtotime when a different 'now' value is used.
     */
    $now = time();
    $timestamp = strtotime($duration_string, $now);
    $timestamp2 = strtotime($duration_string, $now - 100);
    if ($timestamp === FALSE || $timestamp < 0) {

      // Invalid format.
      $form_state
        ->setErrorByName('role_expire', 'Role expiry default duration must be a strtotime-compatible string.');
    }
    elseif ($timestamp < $now) {

      // In the past.
      $form_state
        ->setErrorByName('role_expire', 'Role expiry default duration must be a <strong>future</strong> strtotime-compatible string.');
    }
    elseif ($timestamp == $timestamp2) {

      // This is an absolute (or special) timestamp. That's not allowed (not relative).
      $form_state
        ->setErrorByName('role_expire', 'Role expiry default duration must be a <strong>relative</strong> strtotime-compatible string.');
    }
  }
}