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
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
class EditableTextField extends EditableFormField
|
|
|
|
{
|
|
|
|
|
|
|
|
private static $singular_name = 'Text Field';
|
|
|
|
|
|
|
|
private static $plural_name = 'Text Fields';
|
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'MinLength' => 'Int',
|
|
|
|
'MaxLength' => 'Int',
|
|
|
|
'Rows' => 'Int(1)',
|
|
|
|
'Placeholder' => 'Varchar(255)'
|
|
|
|
);
|
|
|
|
|
|
|
|
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'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
|
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
TextField::create(
|
|
|
|
'Placeholder',
|
|
|
|
_t('EditableTextField.PLACEHOLDER', 'Placeholder')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return TextareaField|TextField
|
|
|
|
*/
|
|
|
|
public function getFormField()
|
|
|
|
{
|
|
|
|
if ($this->Rows > 1) {
|
|
|
|
$field = TextareaField::create($this->Name, $this->EscapedTitle, $this->Default)
|
|
|
|
->setFieldHolderTemplate('UserFormsField_holder')
|
|
|
|
->setTemplate('UserFormsTextareaField')
|
|
|
|
->setRows($this->Rows);
|
|
|
|
} else {
|
|
|
|
$field = TextField::create($this->Name, $this->EscapedTitle, $this->Default)
|
|
|
|
->setFieldHolderTemplate('UserFormsField_holder')
|
|
|
|
->setTemplate('UserFormsField');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->doUpdateFormField($field);
|
|
|
|
|
|
|
|
return $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates a formfield with the additional metadata specified by this field
|
|
|
|
*
|
|
|
|
* @param FormField $field
|
|
|
|
*/
|
|
|
|
protected function updateFormField($field)
|
|
|
|
{
|
|
|
|
parent::updateFormField($field);
|
|
|
|
|
|
|
|
if (is_numeric($this->MinLength) && $this->MinLength > 0) {
|
|
|
|
$field->setAttribute('data-rule-minlength', intval($this->MinLength));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($this->MaxLength) && $this->MaxLength > 0) {
|
|
|
|
if ($field instanceof TextField) {
|
2016-05-16 08:05:05 +02:00
|
|
|
$field->setMaxLength(intval($this->MaxLength));
|
|
|
|
}
|
2016-07-21 07:53:59 +02:00
|
|
|
$field->setAttribute('data-rule-maxlength', intval($this->MaxLength));
|
|
|
|
}
|
2015-08-27 13:54:28 +02:00
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
if ($this->Placeholder) {
|
|
|
|
$field->setAttribute('placeholder', $this->Placeholder);
|
|
|
|
}
|
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|