1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/organization/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/organization/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\organizations\validators; |
10
|
|
|
|
11
|
|
|
use craft\helpers\Json; |
12
|
|
|
use flipbox\organizations\behaviors\OrganizationsAssociatedToUserBehavior; |
13
|
|
|
use flipbox\organizations\Organizations as OrganizationPlugin; |
14
|
|
|
use yii\base\Model; |
15
|
|
|
use yii\validators\Validator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Validates all associated |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class OrganizationsValidator extends Validator |
23
|
|
|
{ |
24
|
|
|
const DEFAULT_MESSAGE = 'Invalid organizations.'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritdoc |
28
|
|
|
*/ |
29
|
|
|
public function init() |
30
|
|
|
{ |
31
|
|
|
parent::init(); |
32
|
|
|
|
33
|
|
|
if ($this->message === null) { |
34
|
|
|
$this->message = OrganizationPlugin::t(static::DEFAULT_MESSAGE); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Validates a single attribute. |
40
|
|
|
* Child classes must implement this method to provide the actual validation logic. |
41
|
|
|
* @param Model|OrganizationsAssociatedToUserBehavior $model the data model to be validated |
42
|
|
|
* @param string $attribute the name of the attribute to be validated. |
43
|
|
|
*/ |
44
|
|
|
public function validateAttribute($model, $attribute) |
45
|
|
|
{ |
46
|
|
|
if (!$model->getOrganizationManager()->isMutated()) { |
|
|
|
|
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$result = $this->validateOrganizations($model->getOrganizations()); |
|
|
|
|
51
|
|
|
if (!empty($result)) { |
52
|
|
|
$this->addError($model, $attribute, $result[0], $result[1]); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $organizations |
58
|
|
|
* @return array|null |
59
|
|
|
*/ |
60
|
|
|
private function validateOrganizations(array $organizations) |
61
|
|
|
{ |
62
|
|
|
$hasError = false; |
63
|
|
|
|
64
|
|
|
foreach ($organizations as $organization) { |
65
|
|
|
if (null === $organization->id && !$organization->validate()) { |
66
|
|
|
$hasError = true; |
67
|
|
|
|
68
|
|
|
OrganizationPlugin::warning( |
69
|
|
|
sprintf( |
70
|
|
|
"Invalid organization: '%s'", |
71
|
|
|
Json::encode($organization->getFirstErrors()) |
72
|
|
|
), |
73
|
|
|
__METHOD__ |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ($hasError) { |
79
|
|
|
return [$this->message, []]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: