You are here

function rate_widget_form_validate in Rate 7

Same name and namespace in other branches
  1. 6.2 rate.admin.inc \rate_widget_form_validate()

Form validate.

File

./rate.admin.inc, line 542
Rating admin

Code

function rate_widget_form_validate($form, &$form_state) {

  // Validate machine readable name
  if (!preg_match('/^[a-z\\_0-9]+$/i', $form_state['values']['name'])) {
    form_set_error('name', t('The machine readable name may only contain alphanumeric characters and underscores.'));
  }

  // Validate vote tag
  if (!preg_match('/^[a-z\\_0-9]+$/i', $form_state['values']['tag'])) {
    form_set_error('tag', t('The vote tag may only contain alphanumeric characters and underscores.'));
  }
  $customizable = TRUE;
  if ($form_state['values']['widget_template'] != 'custom') {
    if ($template = _rate_get_template($form_state['values']['widget_template'])) {
      $customizable = isset($template->customizable) ? $template->customizable : TRUE;
    }
  }
  if ($customizable) {

    // Validate option values
    $used_values = array();
    foreach ($form_state['values'] as $name => $value) {
      if (preg_match('/^value([0-9]+)$/', $name, $match)) {
        if (empty($value)) {
          continue;
        }
        if (!preg_match('/^\\-?[0-9]+$/', $value)) {
          form_set_error($name, t('Values must be integers'));
          continue;
        }
        if (in_array($value, $used_values)) {
          form_set_error($name, t('You may not use the same value twice.'));
        }
        $used_values[] = $value;
        if ($form_state['values']['value_type'] == 'percent') {
          if ($value < 0 || $value > 100) {
            form_set_error($name, t('Percentages must be between 0 and 100'));
          }
        }
      }
    }
    if (!count($used_values)) {
      form_set_error('', t('Each widget must have at least one option.'));
    }
  }
}