diff --git a/engine/modules/validate/entity/Validator.entity.class.php b/engine/modules/validate/entity/Validator.entity.class.php index ebd1875b..262b83aa 100644 --- a/engine/modules/validate/entity/Validator.entity.class.php +++ b/engine/modules/validate/entity/Validator.entity.class.php @@ -47,6 +47,18 @@ abstract class ModuleValidate_EntityValidator extends Entity { * @var null|array */ public $on=null; + /** + * Объект текущей сущности, которая проходит валидацию + * + * @var null|Entity + */ + protected $oEntityCurrent=null; + /** + * Объект текущей сущности, которая проходит валидацию + * + * @var null|string + */ + protected $sFieldCurrent=null; /** * Основной метод валидации @@ -121,6 +133,7 @@ abstract class ModuleValidate_EntityValidator extends Entity { } else { $aFields=$this->fields; } + $this->oEntityCurrent=$oEntity; /** * Запускаем валидацию для каждого поля */ @@ -140,6 +153,7 @@ abstract class ModuleValidate_EntityValidator extends Entity { * @return bool */ public function validateEntityField($oEntity,$sField) { + $this->sFieldCurrent=$sField; /** * Получаем значение поля у сущности через геттер */ @@ -155,5 +169,17 @@ abstract class ModuleValidate_EntityValidator extends Entity { return true; } } + /** + * Возвращает значение поля текущей сущности + * + * @param $sField + * @return mixed|null + */ + protected function getValueOfCurrentEntity($sField) { + if ($this->oEntityCurrent) { + return call_user_func_array(array($this->oEntityCurrent,'get'.func_camelize($sField)),array()); + } + return null; + } } ?> \ No newline at end of file diff --git a/engine/modules/validate/entity/ValidatorCaptcha.entity.class.php b/engine/modules/validate/entity/ValidatorCaptcha.entity.class.php new file mode 100644 index 00000000..bc79c8e6 --- /dev/null +++ b/engine/modules/validate/entity/ValidatorCaptcha.entity.class.php @@ -0,0 +1,50 @@ +allowEmpty && $this->isEmpty($sValue)) { + return true; + } + + if (!isset($_SESSION['captcha_keystring']) or $_SESSION['captcha_keystring']!=strtolower($sValue)) { + return $this->getMessage($this->Lang_Get('validate_captcha_not_valid',null,false),'msg'); + } + return true; + } +} +?> \ No newline at end of file diff --git a/engine/modules/validate/entity/ValidatorCompare.entity.class.php b/engine/modules/validate/entity/ValidatorCompare.entity.class.php new file mode 100644 index 00000000..2e5ef372 --- /dev/null +++ b/engine/modules/validate/entity/ValidatorCompare.entity.class.php @@ -0,0 +1,132 @@ + + * @link http://www.yiiframework.com/ + * @copyright Copyright © 2008-2011 Yii Software LLC + * @license http://www.yiiframework.com/license/ + */ + +/** + * Валидатор сравнения значений + */ +class ModuleValidate_EntityValidatorCompare extends ModuleValidate_EntityValidator { + + /** + * Имя поля для сравнения + * + * @var string + */ + public $compareField; + /** + * Значение для сравнения + * + * @var string + */ + public $compareValue; + /** + * Название поля сущности для сравнения, используется в сообщениях об ошибках + * + * @var string + */ + public $compareLabel; + /** + * Строгое сравнение + * + * @var bool + */ + public $strict=false; + /** + * Допускать или нет пустое значение + * + * @var bool + */ + public $allowEmpty=false; + /** + * Оператор для сравнения + * Доступны: '=' или '==', '!=', '>', '>=', '<', '<=' + * + * @var string + */ + public $operator='='; + + + + /** + * Запуск валидации + * + * @param $sValue Данные для валидации + * + * @return bool|string + */ + public function validate($sValue) { + if($this->allowEmpty && $this->isEmpty($sValue)) { + return true; + } + + /** + * Определяем значение для сравнения + */ + if($this->compareValue!==null or !$this->oEntityCurrent) { + $sCompareLabel=$sCompareValue=$this->compareValue; + } else { + $sCompareField=$this->compareField===null ? $this->sFieldCurrent.'_repeat' : $this->compareField; + $sCompareValue=$this->getValueOfCurrentEntity($sCompareField); + $sCompareLabel=is_null($this->compareLabel) ? $sCompareField : $this->compareLabel; + } + + switch($this->operator) { + case '=': + case '==': + if(($this->strict && $sValue!==$sCompareValue) || (!$this->strict && $sValue!=$sCompareValue)) { + return $this->getMessage($this->Lang_Get('validate_compare_must_repeated',null,false),'msg',array('compare_field'=>$sCompareLabel)); + } + break; + case '!=': + if(($this->strict && $sValue===$sCompareValue) || (!$this->strict && $sValue==$sCompareValue)) { + return $this->getMessage($this->Lang_Get('validate_compare_must_not_equal',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue))); + } + break; + case '>': + if($sValue<=$sCompareValue) { + return $this->getMessage($this->Lang_Get('validate_compare_must_greater',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue))); + } + break; + case '>=': + if($sValue<$sCompareValue) { + return $this->getMessage($this->Lang_Get('validate_compare_must_greater_equal',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue))); + } + break; + case '<': + if($sValue>=$sCompareValue) { + return $this->getMessage($this->Lang_Get('validate_compare_must_less',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue))); + } + break; + case '<=': + if($sValue>$sCompareValue) { + return $this->getMessage($this->Lang_Get('validate_compare_must_less_equal',null,false),'msg',array('compare_field'=>$sCompareLabel,'compare_value'=>htmlspecialchars($sCompareValue))); + } + break; + default: + return $this->getMessage($this->Lang_Get('validate_compare_invalid_operator',null,false),'msg',array('operator'=>$this->operator)); + } + return true; + } +} +?> \ No newline at end of file diff --git a/templates/language/english.php b/templates/language/english.php index c81676a2..2343cb9a 100644 --- a/templates/language/english.php +++ b/templates/language/english.php @@ -916,6 +916,14 @@ return array( 'validate_required_must_be' => 'Field %%field%% must be %%value%%', 'validate_required_cannot_blank' => 'Field %%field%% cannot be blank', 'validate_url_not_valid' => 'Field %%field%% is not a valid URL', + 'validate_captcha_not_valid' => 'Field %%field%% is not a valid code', + 'validate_compare_must_repeated' => 'Field %%field%% must be repeated %%compare_field%%', + 'validate_compare_must_not_equal' => 'Field %%field%% must not be equal to %%compare_value%%', + 'validate_compare_must_greater' => 'Field %%field%% must be greater than %%compare_value%%', + 'validate_compare_must_greater_equal' => 'Field %%field%% must be greater than or equal to %%compare_value%%', + 'validate_compare_must_less' => 'Field %%field%% must be less than %%compare_value%%', + 'validate_compare_must_less_equal' => 'Field %%field%% must be less than or equal to %%compare_value%%', + 'validate_compare_invalid_operator' => 'Field %%field%% invalid operator %%operator%%', /** * Подписка */ diff --git a/templates/language/russian.php b/templates/language/russian.php index b939ac53..f23ef820 100644 --- a/templates/language/russian.php +++ b/templates/language/russian.php @@ -916,6 +916,14 @@ return array( 'validate_required_must_be' => 'Поле %%field%% должно иметь значение %%value%%', 'validate_required_cannot_blank' => 'Поле %%field%% не может быть пустым', 'validate_url_not_valid' => 'Поле %%field%% не соотвествует формату URL адреса', + 'validate_captcha_not_valid' => 'Поле %%field%% содержит неверный код', + 'validate_compare_must_repeated' => 'Поле %%field%% должно повторять %%compare_field%%', + 'validate_compare_must_not_equal' => 'Поле %%field%% не должно повторять %%compare_value%%', + 'validate_compare_must_greater' => 'Поле %%field%% должно быть больше чем %%compare_value%%', + 'validate_compare_must_greater_equal' => 'Поле %%field%% должно быть больше или равно %%compare_value%%', + 'validate_compare_must_less' => 'Поле %%field%% должно быть меньше чем %%compare_value%%', + 'validate_compare_must_less_equal' => 'Поле %%field%% должно быть меньше или равно %%compare_value%%', + 'validate_compare_invalid_operator' => 'У поля %%field%% неверный оператор сравнения %%operator%%', /** * Подписка */