1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
lengthvalidator.php
См. документацию.
1<?php
8
9namespace Bitrix\Main\ORM\Fields\Validators;
10
11use Bitrix\Main\ORM\Fields\Field;
12use Bitrix\Main\ArgumentTypeException;
13use Bitrix\Main\Localization\Loc;
14use Bitrix\Main\ORM\Fields\ScalarField;
15
16Loc::loadMessages(__FILE__);
17
19{
21 protected $min;
22
24 protected $max;
25
27 protected $errorPhraseMinCode = 'MAIN_ENTITY_VALIDATOR_LENGTH_MIN';
28 protected $errorPhraseMin;
29
31 protected $errorPhraseMaxCode = 'MAIN_ENTITY_VALIDATOR_LENGTH_MAX';
32 protected $errorPhraseMax;
33
41 public function __construct($min = 1, $max = null, $errorPhrase = array('MIN' => null, 'MAX' => null))
42 {
43 if ($min !== null)
44 {
45 if (!is_int($min))
46 {
47 throw new ArgumentTypeException('min', 'integer');
48 }
49
50 $this->min = $min;
51 }
52
53 if ($max !== null)
54 {
55 if (!is_int($max))
56 {
57 throw new ArgumentTypeException('max', 'integer');
58 }
59
60 $this->max = $max;
61 }
62
63 if (!empty($errorPhrase['MIN']))
64 {
65 $this->errorPhraseMin = $errorPhrase['MIN'];
66 }
67
68 if (!empty($errorPhrase['MAX']))
69 {
70 $this->errorPhraseMax = $errorPhrase['MAX'];
71 }
72
73 parent::__construct();
74 }
75
87 public function validate($value, $primary, array $row, Field $field)
88 {
89 if ($field instanceof ScalarField && $field->isNullable() && $value === null)
90 {
91 return true;
92 }
93
94 if ($this->min !== null)
95 {
96 if (mb_strlen((string)$value) < $this->min)
97 {
98 $mess = ($this->errorPhraseMin !== null? $this->errorPhraseMin : Loc::getMessage($this->errorPhraseMinCode));
99 return $this->getErrorMessage($value, $field, $mess, array("#MIN_LENGTH#" => $this->min));
100 }
101 }
102
103 if ($this->max !== null)
104 {
105 if (mb_strlen((string)$value) > $this->max)
106 {
107 $mess = ($this->errorPhraseMax !== null? $this->errorPhraseMax : Loc::getMessage($this->errorPhraseMaxCode));
108 return $this->getErrorMessage($value, $field, $mess, array("#MAX_LENGTH#" => $this->max));
109 }
110 }
111
112 return true;
113 }
114
121 public function getMin()
122 {
123 return $this->min;
124 }
125
132 public function getMax()
133 {
134 return $this->max;
135 }
136}
validate($value, $primary, array $row, Field $field)
Определения lengthvalidator.php:87
__construct($min=1, $max=null, $errorPhrase=array('MIN'=> null, 'MAX'=> null))
Определения lengthvalidator.php:41
getErrorMessage($value, ORM\Fields\Field $field, $errorPhrase=null, $additionalTemplates=null)
Определения validator.php:50
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804