You are here

function canvasactions_perspective_distortion_validate in ImageCache Actions 7

Form element validation handler for distortion.

Parameters

array $element: The form element to validate.

array $form_state: The form state.

1 string reference to 'canvasactions_perspective_distortion_validate'
canvasactions_perspective_form in canvasactions/canvasactions.inc
Image effect form callback for the Perspective effect.

File

canvasactions/canvasactions.inc, line 1346

Code

function canvasactions_perspective_distortion_validate($element, &$form_state) {
  $symmetrical = $form_state['values']['data']['symmetry'] === 'symmetrical';
  $element_name = $element['#name'];

  // Do not check opposite distortion if it is hidden in the UI.
  if (!$symmetrical || $element_name === 'data[distortion]') {
    $value = $element['#value'];

    // Check value itself: a number between 0 and 100 (50 if symmetrical):
    $max_value = $symmetrical ? 50 : 100;
    if (!is_numeric($value) || $value < 0 || $value >= $max_value) {
      if ($symmetrical) {
        form_error($element, t('!name must be a value between 0 and 50.', array(
          '!name' => $element['#title'],
        )));
      }
      else {
        form_error($element, t('!name must be a value between 0 and 100.', array(
          '!name' => $element['#title'],
        )));
      }
    }

    // Sum of both distortion values should also be smaller then 100.
    if (!$symmetrical) {
      $other_value_name = $element_name === 'data[distortion]' ? 'opposite_distortion' : 'distortion';
      $other_value = $form_state['values']['data'][$other_value_name];
      if (is_numeric($other_value) && $value + $other_value >= 100) {
        form_error($element, t('The sum of %name and %name2 must be lower then 100.', array(
          '%name' => $element['#title'],
          '%name2' => $other_value_name === 'distortion' ? t('Distortion') : t('Distortion for opposite side'),
        )));
      }
    }
  }
}