2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EditableTextField
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* This control represents a user-defined text field in a user defined form
|
|
|
|
*
|
|
|
|
* @package userforms
|
2008-09-29 05:18:23 +02:00
|
|
|
*/
|
2009-12-07 03:04:20 +01:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
class EditableTextField extends EditableFormField {
|
2009-03-16 22:32:16 +01:00
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $singular_name = 'Text Field';
|
2015-09-11 00:20:06 +02:00
|
|
|
|
2013-04-03 03:34:43 +02:00
|
|
|
private static $plural_name = 'Text Fields';
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'MinLength' => 'Int',
|
|
|
|
'MaxLength' => 'Int',
|
2015-08-27 13:54:28 +02:00
|
|
|
'Rows' => 'Int(1)',
|
2015-09-10 14:08:13 +02:00
|
|
|
'Placeholder' => 'Varchar(255)'
|
2015-07-24 04:37:48 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
private static $defaults = array(
|
|
|
|
'Rows' => 1
|
|
|
|
);
|
|
|
|
|
|
|
|
public function getCMSFields() {
|
|
|
|
$this->beforeUpdateCMSFields(function($fields) {
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
NumericField::create(
|
|
|
|
'Rows',
|
|
|
|
_t('EditableTextField.NUMBERROWS', 'Number of rows')
|
|
|
|
)->setDescription(_t(
|
|
|
|
'EditableTextField.NUMBERROWS_DESCRIPTION',
|
|
|
|
'Fields with more than one row will be generated as a textarea'
|
|
|
|
))
|
|
|
|
);
|
2015-08-27 13:54:28 +02:00
|
|
|
|
2015-09-10 14:08:13 +02:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
TextField::create(
|
|
|
|
'Placeholder',
|
|
|
|
_t('EditableTextField.PLACEHOLDER', 'Placeholder')
|
|
|
|
)
|
|
|
|
);
|
2015-07-24 04:37:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getFieldValidationOptions() {
|
|
|
|
$fields = parent::getFieldValidationOptions();
|
|
|
|
|
|
|
|
$fields->merge(array(
|
|
|
|
FieldGroup::create(
|
|
|
|
_t('EditableTextField.TEXTLENGTH', 'Allowed text length'),
|
|
|
|
array(
|
|
|
|
NumericField::create('MinLength', false),
|
|
|
|
LiteralField::create('RangeLength', _t("EditableTextField.RANGE_TO", "to")),
|
|
|
|
NumericField::create('MaxLength', false)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
return $fields;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
|
|
|
|
2010-09-03 07:06:13 +02:00
|
|
|
/**
|
|
|
|
* @return TextareaField|TextField
|
|
|
|
*/
|
2012-05-04 03:39:08 +02:00
|
|
|
public function getFormField() {
|
2015-07-24 04:37:48 +02:00
|
|
|
if($this->Rows > 1) {
|
2015-08-21 05:21:33 +02:00
|
|
|
$field = TextareaField::create($this->Name, $this->EscapedTitle, $this->Default)
|
|
|
|
->setFieldHolderTemplate('UserFormsField_holder')
|
|
|
|
->setTemplate('UserFormsTextareaField')
|
|
|
|
->setRows($this->Rows);
|
2015-07-24 04:37:48 +02:00
|
|
|
} else {
|
2015-08-21 05:21:33 +02:00
|
|
|
$field = TextField::create($this->Name, $this->EscapedTitle, $this->Default, $this->MaxLength)
|
|
|
|
->setFieldHolderTemplate('UserFormsField_holder')
|
|
|
|
->setTemplate('UserFormsField');
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
2015-08-21 05:21:33 +02:00
|
|
|
|
2015-08-10 07:03:36 +02:00
|
|
|
$this->doUpdateFormField($field);
|
2015-08-21 05:21:33 +02:00
|
|
|
|
2014-12-22 02:49:33 +01:00
|
|
|
return $field;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
2015-08-11 01:40:37 +02:00
|
|
|
|
2009-04-21 05:44:13 +02:00
|
|
|
/**
|
2015-08-11 01:40:37 +02:00
|
|
|
* Updates a formfield with the additional metadata specified by this field
|
2009-04-21 05:44:13 +02:00
|
|
|
*
|
2015-08-11 01:40:37 +02:00
|
|
|
* @param FormField $field
|
2009-04-21 05:44:13 +02:00
|
|
|
*/
|
2015-08-11 01:40:37 +02:00
|
|
|
protected function updateFormField($field) {
|
|
|
|
parent::updateFormField($field);
|
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
if($this->MinLength) {
|
2015-08-11 01:40:37 +02:00
|
|
|
$field->setAttribute('data-rule-minlength', $this->MinLength);
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:48 +02:00
|
|
|
if($this->MaxLength) {
|
2015-08-11 01:40:37 +02:00
|
|
|
$field->setAttribute('data-rule-maxlength', $this->MaxLength);
|
2014-04-16 01:48:10 +02:00
|
|
|
}
|
2015-08-27 13:54:28 +02:00
|
|
|
|
2015-09-10 14:08:13 +02:00
|
|
|
if($this->Placeholder) {
|
|
|
|
$field->setAttribute('placeholder', $this->Placeholder);
|
|
|
|
}
|
2009-04-21 05:44:13 +02:00
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|