1 | <?php |
||
24 | trait ValidatorTrait |
||
25 | { |
||
26 | /** |
||
27 | * Will translate every message it [[]] used and fire some events. |
||
28 | */ |
||
29 | use TranslatorTrait, EventsTrait; |
||
30 | |||
31 | /** |
||
32 | * @var ValidatorInterface |
||
33 | */ |
||
34 | private $validator = null; |
||
35 | |||
36 | /** |
||
37 | * @whatif private |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $errors = []; |
||
41 | |||
42 | /** |
||
43 | * Fields (data) to be validated. Named like that for convenience. |
||
44 | * |
||
45 | * @whatif private |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $fields = []; |
||
49 | |||
50 | /** |
||
51 | * Validation rules defined in validator format. Named like that for convenience. |
||
52 | * |
||
53 | * @var array |
||
54 | */ |
||
55 | protected $validates = []; |
||
56 | |||
57 | /** |
||
58 | * Attach custom validator to model. |
||
59 | * |
||
60 | * @param ValidatorInterface $validator |
||
61 | */ |
||
62 | public function setValidator(ValidatorInterface $validator) |
||
66 | |||
67 | /** |
||
68 | * Check if context data is valid. |
||
69 | * |
||
70 | * @return bool |
||
71 | */ |
||
72 | public function isValid() |
||
78 | |||
79 | /** |
||
80 | * Check if context data has errors. |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | public function hasErrors() |
||
88 | |||
89 | /** |
||
90 | * List of errors associated with parent field, every field should have only one error assigned. |
||
91 | * |
||
92 | * @param bool $reset Re-validate object. |
||
93 | * @return array |
||
94 | */ |
||
95 | public function getErrors($reset = false) |
||
96 | { |
||
97 | $this->validate($reset); |
||
98 | |||
99 | $errors = []; |
||
100 | foreach ($this->errors as $field => $error) { |
||
101 | if ( |
||
102 | is_string($error) |
||
103 | && substr($error, 0, 2) == TranslatorInterface::I18N_PREFIX |
||
104 | && substr($error, -2) == TranslatorInterface::I18N_POSTFIX |
||
105 | ) { |
||
106 | //We will localize only messages embraced with [[ and ]] |
||
107 | $error = $this->say($error); |
||
108 | } |
||
109 | |||
110 | $errors[$field] = $error; |
||
111 | } |
||
112 | |||
113 | return $errors; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Get associated instance of validator or return new one. |
||
118 | * |
||
119 | * @return ValidatorInterface |
||
120 | * @throws SugarException |
||
121 | */ |
||
122 | protected function validator() |
||
130 | |||
131 | /** |
||
132 | * Validate data using associated validator. |
||
133 | * |
||
134 | * @param bool $reset Implementation might reset validation if needed. |
||
135 | * @return bool |
||
136 | * @throws ValidationException |
||
137 | * @throws SugarException |
||
138 | * @event validated(ValidatedEvent) |
||
139 | */ |
||
140 | protected function validate($reset = false) |
||
155 | |||
156 | /** |
||
157 | * Attach error to data field. Internal method to be used in validations. |
||
158 | * |
||
159 | * @param string $field |
||
160 | * @param string $message |
||
161 | */ |
||
162 | protected function setError($field, $message) |
||
166 | |||
167 | /** |
||
168 | * Check if desired field caused some validation error. |
||
169 | * |
||
170 | * @param string $field |
||
171 | * @return bool |
||
172 | */ |
||
173 | protected function hasError($field) |
||
177 | |||
178 | /** |
||
179 | * Create instance of ValidatorInterface. |
||
180 | * |
||
181 | * @param array $rules Non empty rules will initiate validator. |
||
182 | * @param ContainerInterface $container Will fall back to global container. |
||
183 | * @return ValidatorInterface |
||
184 | * @throws SugarException |
||
185 | * @event validator(ValidatorEvent) |
||
186 | */ |
||
187 | protected function createValidator( |
||
222 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.