1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Regex |
4
|
|
|
* |
5
|
|
|
* @category AcsiWidget\Renderer\JqueryValidate\Rule |
6
|
|
|
* @package AcsiWidget\Renderer\JqueryValidate\Rule |
7
|
|
|
* @copyright 2016 ACSI Holding bv (http://www.acsi.eu) |
8
|
|
|
* @version SVN: $Id: $ |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace StrokerForm\Renderer\JqueryValidate\Rule; |
12
|
|
|
|
13
|
|
|
use Zend\Form\ElementInterface; |
14
|
|
|
use Zend\Validator\ValidatorInterface; |
15
|
|
|
use Zend\Validator\Regex as RegexValidator; |
16
|
|
|
|
17
|
|
|
class Regex extends AbstractRule |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get the validation rules |
22
|
|
|
* |
23
|
|
|
* @param ValidatorInterface $validator |
24
|
|
|
* @param ElementInterface $element |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function getRules(ValidatorInterface $validator, ElementInterface $element = null) |
28
|
|
|
{ |
29
|
|
|
/**@var RegexValidator $validator */ |
30
|
|
|
$pattern = $validator->getPattern(); |
31
|
|
|
$modifier = ''; |
32
|
|
|
|
33
|
|
|
// Only i, m and g modifier are compatible with Ecmascript regexp |
34
|
|
|
if (preg_match('/\/(.*)\/([img])?/', $pattern, $matches)) { |
35
|
|
|
$pattern = $matches[1]; |
36
|
|
|
if (isset($matches[2])) { |
37
|
|
|
$modifier = $matches[2]; |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return ['regex' => [$pattern, $modifier]]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the validation message |
46
|
|
|
* |
47
|
|
|
* @param ValidatorInterface $validator |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function getMessages(ValidatorInterface $validator) |
51
|
|
|
{ |
52
|
|
|
return ['regex' => $this->translateMessage('Field does not match expected pattern')]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ValidatorInterface $validator |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
public function canHandle(ValidatorInterface $validator) |
60
|
|
|
{ |
61
|
|
|
return $validator instanceof RegexValidator; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|