1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* EditableTextField |
4
|
|
|
* |
5
|
|
|
* This control represents a user-defined text field in a user defined form |
6
|
|
|
* |
7
|
|
|
* @package userforms |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class EditableTextField extends EditableFormField { |
|
|
|
|
11
|
|
|
|
12
|
|
|
private static $singular_name = 'Text Field'; |
|
|
|
|
13
|
|
|
|
14
|
|
|
private static $plural_name = 'Text Fields'; |
|
|
|
|
15
|
|
|
|
16
|
|
|
private static $db = array( |
|
|
|
|
17
|
|
|
'MinLength' => 'Int', |
18
|
|
|
'MaxLength' => 'Int', |
19
|
|
|
'Rows' => 'Int(1)', |
20
|
|
|
'Placeholder' => 'Varchar(255)' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $defaults = array( |
|
|
|
|
24
|
|
|
'Rows' => 1 |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
public function getCMSFields() { |
28
|
|
|
$this->beforeUpdateCMSFields(function($fields) { |
29
|
|
|
$fields->addFieldToTab( |
30
|
|
|
'Root.Main', |
31
|
|
|
NumericField::create( |
32
|
|
|
'Rows', |
33
|
|
|
_t('EditableTextField.NUMBERROWS', 'Number of rows') |
34
|
|
|
)->setDescription(_t( |
35
|
|
|
'EditableTextField.NUMBERROWS_DESCRIPTION', |
36
|
|
|
'Fields with more than one row will be generated as a textarea' |
37
|
|
|
)) |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$fields->addFieldToTab( |
41
|
|
|
'Root.Main', |
42
|
|
|
TextField::create( |
43
|
|
|
'Placeholder', |
44
|
|
|
_t('EditableTextField.PLACEHOLDER', 'Placeholder') |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
}); |
48
|
|
|
|
49
|
|
|
return parent::getCMSFields(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return FieldList |
54
|
|
|
*/ |
55
|
|
|
public function getFieldValidationOptions() { |
56
|
|
|
$fields = parent::getFieldValidationOptions(); |
57
|
|
|
|
58
|
|
|
$fields->merge(array( |
59
|
|
|
FieldGroup::create( |
60
|
|
|
_t('EditableTextField.TEXTLENGTH', 'Allowed text length'), |
61
|
|
|
array( |
62
|
|
|
NumericField::create('MinLength', false), |
63
|
|
|
LiteralField::create('RangeLength', _t("EditableTextField.RANGE_TO", "to")), |
64
|
|
|
NumericField::create('MaxLength', false) |
65
|
|
|
) |
66
|
|
|
) |
67
|
|
|
)); |
68
|
|
|
|
69
|
|
|
return $fields; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return TextareaField|TextField |
74
|
|
|
*/ |
75
|
|
|
public function getFormField() { |
76
|
|
|
if($this->Rows > 1) { |
|
|
|
|
77
|
|
|
$field = TextareaField::create($this->Name, $this->EscapedTitle, $this->Default) |
|
|
|
|
78
|
|
|
->setFieldHolderTemplate('UserFormsField_holder') |
79
|
|
|
->setTemplate('UserFormsTextareaField') |
80
|
|
|
->setRows($this->Rows); |
|
|
|
|
81
|
|
|
} else { |
82
|
|
|
$field = TextField::create($this->Name, $this->EscapedTitle, $this->Default) |
|
|
|
|
83
|
|
|
->setFieldHolderTemplate('UserFormsField_holder') |
84
|
|
|
->setTemplate('UserFormsField'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->doUpdateFormField($field); |
88
|
|
|
|
89
|
|
|
return $field; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Updates a formfield with the additional metadata specified by this field |
94
|
|
|
* |
95
|
|
|
* @param FormField $field |
96
|
|
|
*/ |
97
|
|
|
protected function updateFormField($field) { |
98
|
|
|
parent::updateFormField($field); |
99
|
|
|
|
100
|
|
|
if(is_numeric($this->MinLength) && $this->MinLength > 0) { |
|
|
|
|
101
|
|
|
$field->setAttribute('data-rule-minlength', intval($this->MinLength)); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if(is_numeric($this->MaxLength) && $this->MaxLength > 0) { |
|
|
|
|
105
|
|
|
if($field instanceof TextField) { |
106
|
|
|
$field->setMaxLength(intval($this->MaxLength)); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
$field->setAttribute('data-rule-maxlength', intval($this->MaxLength)); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if($this->Placeholder) { |
|
|
|
|
112
|
|
|
$field->setAttribute('placeholder', $this->Placeholder); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.