Completed
Push — master ( 4409a5...8614c9 )
by Martijn van
02:32
created

ConsoleForm::getCustomValidator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * Class ConsoleForm
5
 */
6
class ConsoleForm extends Form
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * @var ContentController
10
     */
11
    protected $controller;
12
13
    /**
14
     * @param Controller $controller
15
     * @param string $name
16
     */
17
    public function __construct(Controller $controller, $name)
18
    {
19
        $this->controller = $controller;
0 ignored issues
show
Documentation Bug introduced by
$controller is of type object<Controller>, but the property $controller was declared to be of type object<ContentController>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
20
21
        $fields    = $this->getCustomFields();
22
        $actions   = $this->getCustomActions();
23
        $validator = $this->getCustomValidator($fields);
24
25
        parent::__construct($controller, $name, $fields, $actions, $validator);
26
27
        $this->setFormAction('/console/'.$name);
28
    }
29
30
    /**
31
     * @param array $data
32
     * @param ConsoleForm $form
33
     * @param SS_HTTPRequest $request
34
     */
35
    public function submitForm($data, ConsoleForm $form, SS_HTTPRequest $request)
0 ignored issues
show
Unused Code introduced by
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        //TODO : do something with data or form
38
39
        return $this->controller->redirect('/console/index/'.$data['Name']);
40
    }
41
42
    /**
43
     * @return FieldList
44
     */
45
    public function getCustomFields()
46
    {
47
        $fields = new FieldList(
48
            new TextField('Name', 'Name')
49
        );
50
51
        return $fields;
52
    }
53
54
    /**
55
     * @return FieldList
56
     */
57
    public function getCustomActions()
58
    {
59
        $fields = new FieldList(
60
            new FormAction('submitForm', _t('Site.Submit', 'Submit'))
61
        );
62
63
        return $fields;
64
    }
65
66
    public function getCustomValidator(FieldList $fields)
67
    {
68
        $required = [];
69
70
        $this->setRequiredFieldsLabels($required, $fields);
71
72
        return RequiredFields::create($required);
73
    }
74
75
    /**
76
     * Add required stars to field labels
77
     *
78
     * @param array $required
79
     * @param FieldList $fields
80
     */
81
    protected function setRequiredFieldsLabels($required, FieldList $fields)
82
    {
83
        foreach ((array)$required as $req) {
84
            $field = $fields->dataFieldByName($req);
85
            if ($field && $title = trim($field->Title())) {
86
                if (!$field->getCustomValidationMessage()) {
87
                    $message = strip_tags($title) . ' ' . _t('Site.IsRequired', ' is required');
88
                    $field->setCustomValidationMessage($message);
89
                }
90
                $field->setTitle($title . ' *');
91
            }
92
        }
93
    }
94
95
}
96