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:
<?php
namespace MvcCore\Ext\Form\Validators;
require_once(__DIR__.'/../../Form.php');
require_once(__DIR__.'/../Core/Validator.php');
require_once(__DIR__.'/../Core/Field.php');
require_once(__DIR__.'/../Core/View.php');
use
MvcCore\Ext\Form,
MvcCore\Ext\Form\Core;
class Pattern extends Core\Validator
{
public function Validate ($submitValue, $fieldName, \MvcCore\Ext\Form\Core\Field & $field) {
$safeValue = '';
$submitValue = trim($submitValue);
if (isset($field->Pattern) && !is_null($field->Pattern)) {
$pattern = $field->Pattern;
if (mb_strpos($pattern, "#") !== 0) {
$pattern = "#" . $pattern . "#";
}
preg_match($pattern, $submitValue, $matches);
if ($matches) {
$safeValue = $submitValue;
}
} else {
$safeValue = $submitValue;
}
if (mb_strlen($safeValue) !== mb_strlen($submitValue)) {
$this->addError(
$field,
Form::$DefaultMessages[Form::INVALID_FORMAT],
function ($msg, $args) use (& $field) {
$args[] = $field->Pattern;
return Core\View::Format($msg, $args);
}
);
}
return $safeValue;
}
}