GridFieldDeleteRelationsValidator   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 25
c 3
b 0
f 0
dl 0
loc 45
rs 10
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B php() 0 43 10
1
<?php
2
3
namespace Signify\Forms\Validators;
4
5
use Signify\Forms\GridField\GridFieldDeleteRelationsButton;
6
use SilverStripe\Forms\Validator;
7
8
class GridFieldDeleteRelationsValidator extends Validator
9
{
10
    public function php($data)
11
    {
12
        $valid = true;
13
        $filters = array();
14
        // Check for checked filter checkboxes.
15
        foreach ($data as $key => $value) {
16
            if (preg_match('/' . GridFieldDeleteRelationsButton::FILTER_BY_SUFFIX . '$/', $key) && $value) {
17
                $filters[] = $key;
18
            }
19
        }
20
21
        // If the delete all checkbox is checked, no other filters can be checked.
22
        if (!empty($filters) && !empty($data[GridFieldDeleteRelationsButton::DELETE_ALL])) {
23
            $message = _t(
24
                GridFieldDeleteRelationsButton::class . '.VALIDATION_TooManyFilters',
25
                'A filter checkbox and "Delete all" cannot be checked simultaneously.'
26
            );
27
            $filters[] = GridFieldDeleteRelationsButton::DELETE_ALL;
28
            foreach ($filters as $fieldName) {
29
                $this->validationError($fieldName, $message);
30
            }
31
            $valid = false;
32
        }
33
34
        // At least one checkbox must be checked.
35
        if (empty($filters) && empty($data[GridFieldDeleteRelationsButton::DELETE_ALL])) {
36
            $message = _t(
37
                GridFieldDeleteRelationsButton::class . '.VALIDATION_RequireFilters',
38
                'At least one filter checkbox or "Delete all" must be checked.'
39
            );
40
            $this->validationError(GridFieldDeleteRelationsButton::DELETE_ALL, $message);
41
            $valid = false;
42
        }
43
44
        // Add a message to the form itself.
45
        if (!$valid) {
46
            $this->validationError('', _t(
47
                GridFieldDeleteRelationsButton::class . '.VALIDATION_FormMessage',
48
                'Please correct the validation errors.'
49
            ));
50
        }
51
52
        return $valid;
53
    }
54
}
55