-
Notifications
You must be signed in to change notification settings - Fork 2
/
class.php
78 lines (70 loc) · 2.51 KB
/
class.php
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
<?php
namespace YLab\Validation\Components;
use Bitrix\Main\UserTable;
use YLab\Validation\ComponentValidation;
use YLab\Validation\ValidatorHelper;
/**
* Class ValidationTestComponent
* Компонент пример использования модуля ylab.validation в разработке
*
* @package YLab\Validation\Components
*/
class ValidationTestComponent extends ComponentValidation
{
/**
* ValidationTestComponent constructor.
* @param \CBitrixComponent|null $component
* @param string $sFile
* @throws \Bitrix\Main\IO\InvalidPathException
* @throws \Bitrix\Main\SystemException
* @throws \Exception
*/
public function __construct(\CBitrixComponent $component = null, $sFile = __FILE__)
{
parent::__construct($component, $sFile);
}
/**
* @return mixed|void
* @throws \Exception
*/
public function executeComponent()
{
/**
* При необходимости в компоненте можно реализовать дополнительные правила валидации, например:
*/
$this->oValidator->addExtension('user_exists', function($attribute, $value, $parameters, $validator) {
$arValidate = UserTable::getList([
'select' => ['ID'],
'filter' => ['=ID' => $value],
'limit' => 1
])->fetch();
return $arValidate['ID'] ? true : false;
});
/**
* Непосредственно валидация и действия при успехе и фейле
*/
if ($this->oRequest->isPost() && check_bitrix_sessid()) {
$this->oValidator->setData($this->oRequest->toArray());
if ($this->oValidator->passes()) {
$this->arResult['SUCCESS'] = true;
} else {
$this->arResult['ERRORS'] = ValidatorHelper::errorsToArray($this->oValidator);
}
}
$this->includeComponentTemplate();
}
/**
* @return array
*/
protected function rules()
{
/**
* Перед формированием массива правил валидации мы можем вытащить все необходимые данные из различных источников
*/
return [
'user' => 'required|numeric|user_exists',
'date' => 'required|date_format:d.m.Y',
'rating' => 'required|min:1|max:5|numeric'
];
}
}