forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategoryRule.php
More file actions
57 lines (49 loc) · 1.43 KB
/
CategoryRule.php
File metadata and controls
57 lines (49 loc) · 1.43 KB
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
<?php
namespace ProcessMaker\Validation;
use Illuminate\Contracts\Validation\ImplicitRule;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
/**
* Must implement ImplicitRule because this always needs
* to be run, even if the field is empty.
*/
class CategoryRule implements ImplicitRule
{
public function __construct($model)
{
$this->model = $model;
}
public function passes($attribute, $value)
{
$type = explode('_', $attribute)[0];
$class = '\\ProcessMaker\\Models\\' . ucfirst($type) . 'Category';
/**
* If the model has previously been saved to the database,
* it's safe to assume that the category exists and is not
* a required parameter for an update.
*/
if (empty($value)) {
if ($this->model && $this->model->exists) {
return true;
} else {
validator()->validate([], [$attribute => 'required']);
}
}
if (strpos($value, ',') !== false) {
$ids = explode(',', $value);
} else {
$ids = [$value];
}
foreach ($ids as $id) {
if (!$class::where('id', $id)->exists()) {
return false;
}
}
return true;
}
public function message()
{
return 'Invalid category';
}
}