Completed
Pull Request — master (#520)
by Nic
32:38
created

EditableDropdown::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 0
1
<?php
2
3
/**
4
 * EditableDropdown
5
 *
6
 * Represents a modifiable dropdown (select) box on a form
7
 *
8
 * @property bool $UseEmptyString
9
 * @property string $EmptyString
10
 *
11
 * @package userforms
12
 */
13
class EditableDropdown extends EditableMultipleOptionField
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...
14
{
15
16
    private static $singular_name = 'Dropdown Field';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $singular_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    private static $plural_name = 'Dropdowns';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $plural_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
20
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
21
        'UseEmptyString' => 'Boolean',
22
        'EmptyString' => 'Varchar(255)',
23
    );
24
25
    /**
26
     * @return FieldList
27
     */
28
    public function getCMSFields()
29
    {
30
        $fields = parent::getCMSFields();
31
32
        $fields->addFieldToTab(
33
            'Root.Main',
34
            CheckboxField::create('UseEmptyString')
35
                ->setTitle('Set default empty string')
36
        );
37
38
        $fields->addFieldToTab(
39
            'Root.Main',
40
            TextField::create('EmptyString')
41
                ->setTitle('Empty String')
42
        );
43
44
        $fields->removeByName('Default');
45
46
        return $fields;
47
    }
48
49
    /**
50
     * @return ValidationResult
51
     */
52
    public function validate()
53
    {
54
        $result = parent::validate();
55
56
        if ($this->UseEmptyString && !$this->EmptyString) {
57
            $result->error('Please enter the default option text');
58
        }
59
60
        return $result;
61
    }
62
63
    /**
64
     * @return DropdownField
65
     */
66
    public function getFormField()
67
    {
68
        $field = DropdownField::create($this->Name, $this->EscapedTitle, $this->getOptionsMap())
0 ignored issues
show
Documentation introduced by
The property EscapedTitle does not exist on object<EditableDropdown>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69
            ->setFieldHolderTemplate('UserFormsField_holder')
70
            ->setTemplate('UserFormsDropdownField');
71
72
        if ($this->UseEmptyString) {
73
            $field->setEmptyString($this->EmptyString);
74
        }
75
76
        // Set default
77
        $defaultOption = $this->getDefaultOptions()->first();
78
        if ($defaultOption) {
79
            $field->setValue($defaultOption->EscapedTitle);
80
        }
81
        $this->doUpdateFormField($field);
82
        return $field;
83
    }
84
85
    public function getSelectorField(EditableCustomRule $rule, $forOnLoad = false)
86
    {
87
        return "$(\"select[name='{$this->Name}']\")";
88
    }
89
}
90