1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103:
<?php
namespace MvcCore\Ext\Form\Core;
require_once('Configuration.php');
require_once('Field.php');
abstract class Validator
{
protected $Form = NULL;
protected $Controller = NULL;
protected $Translate = FALSE;
protected $Translator = NULL;
protected static $validatorsClassNameTemplate = '\MvcCore\Ext\Form\Validators\{ValidatorName}';
protected static $instances = array();
public static function Create (\MvcCore\Ext\Form\Core\Configuration & $form, $validatorName = '') {
if (!isset(static::$instances[$validatorName])) {
$localValidatorClassName = strpos($validatorName, '_') === FALSE && strpos($validatorName, '\\') === FALSE;
if ($localValidatorClassName) {
$className = str_replace('{ValidatorName}', $validatorName, static::$validatorsClassNameTemplate);
} else {
$className = $validatorName;
}
static::$instances[$validatorName] = new $className($form);
}
return static::$instances[$validatorName];
}
public function __construct (\MvcCore\Ext\Form\Core\Configuration & $form) {
$this->Form = & $form;
$this->Controller = & $form->Controller;
$this->Translate = $form->Translate;
if ($this->Translate) $this->Translator = & $form->Translator;
}
public function Validate ($submitValue, $fieldName, \MvcCore\Ext\Form\Core\Field & $field) {
return $submitValue;
}
protected function addError (\MvcCore\Ext\Form\Core\Field & $field, $msg = '', callable $replaceCall = NULL) {
$replacing = !is_null($replaceCall);
$label = '';
if ($replacing) $label = $field->Label ? $field->Label : $field->Name;
if ($this->Translate) {
$msg = call_user_func($this->Translator, $msg);
if ($replacing) {
$label = $field->Label ? call_user_func($this->Translator, $field->Label) : $field->Name;
}
}
if ($replacing) {
$msg = call_user_func($replaceCall, $msg, array($label));
}
$this->Form->AddError(
$msg, $field->Name
);
}
}