Skip to content

Instantly share code, notes, and snippets.

@dreadwarrior
Created February 14, 2012 15:24
Show Gist options
  • Save dreadwarrior/1827537 to your computer and use it in GitHub Desktop.
Save dreadwarrior/1827537 to your computer and use it in GitHub Desktop.
TYPO3 extbase CompareFieldValidator
<?php
/***************************************************************
* Copyright notice
*
* (c) 2011 Thomas Juhnke (tommy@van-tomas.de)
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
* A copy is found in the textfile GPL.txt and important notices to the license
* from the author is found in LICENSE.txt distributed with these scripts.
*
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Validates two input fields by comparison
*
* @author Thomas Juhnke <tommy@van-tomas.de>
* @package Tx_MyExt
* @subpackage ErrorChecks
*/
class Tx_MyExt_ErrorCheck_CompareField extends Tx_Formhandler_AbstractErrorCheck {
/**
* Validates that items of a specified array field are of a specific type or required
*
* @return string If the check failed, the string contains the name of the failed check plus the parameters and values.
*/
public function check() {
$checkFailed = '';
$value1 = $this->gp[$this->formFieldName];
$value2 = $this->utilityFuncs->getSingle($this->settings['params'], 'field');
$operator = $this->utilityFuncs->getSingle($this->settings['params'], 'operator');
$operator = trim((is_null($operator) ? '==' : $operator, '\'" ');
if ($value2) {
$valid = false;
switch ($operator) {
case '>':
$valid = $value1 > $value2;
break;
case '>=':
$valid = $value1 >= $value2;
break;
case '==':
$valid = $value1 == $value2;
break;
case '<=':
$valid = $value1 <= $value2;
break;
case '<':
$valid = $value1 < $value2;
break;
default:
throw new Exception(sprintf('Invalid comparison operator "%s"', $operator));
break;
}
if (!$valid) {
$checkFailed = $this->getCheckFailed();
}
}
// sprintf('The value of field "%s" could not be fetched', $value2);
return $checkFailed;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment