Completed
Push — master ( 8614c9...da2a7e )
by Martijn van
02:26
created

ConsoleForm::setRequiredFieldsLabels()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.2
cc 4
eloc 6
nc 3
nop 2
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
                $this->setCustomValidationMessage($field, $title);
87
                $field->setTitle($title . ' *');
88
            }
89
        }
90
    }
91
92
    /**
93
     * Add is $Field is required message.
94
     *
95
     * @param FormField $field
96
     * @param $title
97
     */
98
    protected function setCustomValidationMessage(FormField $field, $title)
99
    {
100
        if (!$field->getCustomValidationMessage()) {
101
            $message = strip_tags($title) . ' ' . _t('Site.IsRequired', ' is required');
102
            $field->setCustomValidationMessage($message);
103
        }
104
    }
105
106
}
107