2008-09-29 05:18:23 +02:00
|
|
|
<?php
|
2017-08-09 01:55:09 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\UserForms\Model\EditableFormField;
|
|
|
|
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\FieldGroup;
|
2019-05-17 04:55:49 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\FormField;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
2017-08-09 01:55:09 +02:00
|
|
|
use SilverStripe\Forms\TextareaField;
|
|
|
|
use SilverStripe\Forms\TextField;
|
2024-09-23 04:40:23 +02:00
|
|
|
use SilverStripe\Core\Validation\ValidationResult;
|
2017-08-11 01:33:06 +02:00
|
|
|
use SilverStripe\UserForms\Model\EditableFormField;
|
2017-08-09 01:55:09 +02:00
|
|
|
|
2008-09-29 05:18:23 +02:00
|
|
|
/**
|
|
|
|
* EditableTextField
|
2009-04-17 04:26:40 +02:00
|
|
|
*
|
|
|
|
* This control represents a user-defined text field in a user defined form
|
|
|
|
*
|
|
|
|
* @package userforms
|
2021-02-26 04:13:23 +01:00
|
|
|
* @property string $Autocomplete
|
|
|
|
* @property int $MaxLength
|
|
|
|
* @property int $MinLength
|
|
|
|
* @property int $Rows
|
2008-09-29 05:18:23 +02: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';
|
|
|
|
|
2017-04-28 10:31:51 +02:00
|
|
|
private static $has_placeholder = true;
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $autocomplete_options = [
|
2016-12-13 22:12:46 +01:00
|
|
|
'off' => 'Off',
|
|
|
|
'on' => 'On',
|
|
|
|
'name' => 'Full name',
|
|
|
|
'honorific-prefix' => 'Prefix or title',
|
|
|
|
'given-name' => 'First name',
|
|
|
|
'additional-name' => 'Additional name',
|
|
|
|
'family-name' => 'Family name',
|
|
|
|
'honorific-suffix' => 'Suffix (e.g Jr.)',
|
|
|
|
'nickname' => 'Nickname',
|
2017-08-11 01:33:06 +02:00
|
|
|
'email' => 'Email',
|
2016-12-13 22:12:46 +01:00
|
|
|
'organization-title' => 'Job title',
|
|
|
|
'organization' => 'Organization',
|
|
|
|
'street-address' => 'Street address',
|
|
|
|
'address-line1' => 'Address line 1',
|
|
|
|
'address-line2' => 'Address line 2',
|
|
|
|
'address-line3' => 'Address line 3',
|
|
|
|
'address-level1' => 'Address level 1',
|
|
|
|
'address-level2' => 'Address level 2',
|
|
|
|
'address-level3' => 'Address level 3',
|
|
|
|
'address-level4' => 'Address level 4',
|
|
|
|
'country' => 'Country',
|
|
|
|
'country-name' => 'Country name',
|
|
|
|
'postal-code' => 'Postal code',
|
|
|
|
'bday' => 'Birthday',
|
|
|
|
'sex' => 'Gender identity',
|
|
|
|
'tel' => 'Telephone number',
|
|
|
|
'url' => 'Home page'
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-12-13 22:12:46 +01:00
|
|
|
|
2017-04-28 00:22:15 +02:00
|
|
|
protected $jsEventHandler = 'keyup';
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $db = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'MinLength' => 'Int',
|
|
|
|
'MaxLength' => 'Int',
|
|
|
|
'Rows' => 'Int(1)',
|
2016-12-13 22:12:46 +01:00
|
|
|
'Autocomplete' => 'Varchar(255)'
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
2016-07-21 07:53:59 +02:00
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
private static $defaults = [
|
2016-07-21 07:53:59 +02:00
|
|
|
'Rows' => 1
|
2017-08-11 01:33:06 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
private static $table_name = 'EditableTextField';
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-08-11 01:33:06 +02:00
|
|
|
$this->beforeUpdateCMSFields(function ($fields) {
|
|
|
|
$fields->addFieldsToTab(
|
2016-07-21 07:53:59 +02:00
|
|
|
'Root.Main',
|
2017-08-11 01:33:06 +02:00
|
|
|
[
|
|
|
|
NumericField::create(
|
|
|
|
'Rows',
|
|
|
|
_t(__CLASS__.'.NUMBERROWS', 'Number of rows')
|
|
|
|
)->setDescription(_t(
|
|
|
|
__CLASS__.'.NUMBERROWS_DESCRIPTION',
|
|
|
|
'Fields with more than one row will be generated as a textarea'
|
|
|
|
)),
|
|
|
|
DropdownField::create(
|
|
|
|
'Autocomplete',
|
|
|
|
_t(__CLASS__.'.AUTOCOMPLETE', 'Autocomplete'),
|
|
|
|
$this->config()->get('autocomplete_options')
|
|
|
|
)->setDescription(_t(
|
|
|
|
__CLASS__.'.AUTOCOMPLETE_DESCRIPTION',
|
|
|
|
'Supported browsers will attempt to populate this field automatically with the users information, use to set the value populated'
|
|
|
|
))
|
|
|
|
]
|
2016-07-21 07:53:59 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
return parent::getCMSFields();
|
|
|
|
}
|
|
|
|
|
2019-01-15 23:15:53 +01:00
|
|
|
/**
|
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
$result = parent::validate();
|
|
|
|
|
|
|
|
if ($this->MinLength > $this->MaxLength) {
|
2019-01-16 01:50:16 +01:00
|
|
|
$result->addError(_t(
|
2020-12-01 22:01:50 +01:00
|
|
|
__CLASS__ . '.MINMAXLENGTHCHECK',
|
2019-01-16 03:08:42 +01:00
|
|
|
'Minimum length should be less than the Maximum length.'
|
|
|
|
));
|
2019-01-15 23:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-07-21 07:53:59 +02:00
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getFieldValidationOptions()
|
|
|
|
{
|
|
|
|
$fields = parent::getFieldValidationOptions();
|
|
|
|
|
2017-08-11 01:33:06 +02:00
|
|
|
$fields->merge([
|
2016-07-21 07:53:59 +02:00
|
|
|
FieldGroup::create(
|
2017-08-11 01:33:06 +02:00
|
|
|
_t(__CLASS__.'.TEXTLENGTH', 'Allowed text length'),
|
|
|
|
[
|
2019-05-17 05:09:50 +02:00
|
|
|
NumericField::create('MinLength', false)
|
|
|
|
->setAttribute('aria-label', _t(__CLASS__ . '.MIN_LENGTH', 'Minimum text length')),
|
|
|
|
LiteralField::create(
|
|
|
|
'RangeLength',
|
|
|
|
'<span class="userform-field__allowed-length-separator">'
|
|
|
|
. _t(__CLASS__ . '.RANGE_TO', 'to')
|
|
|
|
. '</span>'
|
|
|
|
),
|
2016-07-21 07:53:59 +02:00
|
|
|
NumericField::create('MaxLength', false)
|
2019-05-17 05:09:50 +02:00
|
|
|
->setAttribute('aria-label', _t(__CLASS__ . '.MAX_LENGTH', 'Maximum text length')),
|
2017-08-11 01:33:06 +02:00
|
|
|
]
|
2019-05-17 05:09:50 +02:00
|
|
|
)->addExtraClass('userform-field__allowed-length')
|
2017-08-11 01:33:06 +02:00
|
|
|
]);
|
2016-07-21 07:53:59 +02:00
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return TextareaField|TextField
|
|
|
|
*/
|
|
|
|
public function getFormField()
|
|
|
|
{
|
|
|
|
if ($this->Rows > 1) {
|
2018-03-23 04:23:51 +01:00
|
|
|
$field = TextareaField::create($this->Name, $this->Title ?: false, $this->Default)
|
2017-08-14 02:29:57 +02:00
|
|
|
->setFieldHolderTemplate(EditableFormField::class . '_holder')
|
|
|
|
->setTemplate(str_replace('EditableTextField', 'EditableTextareaField', __CLASS__))
|
2016-07-21 07:53:59 +02:00
|
|
|
->setRows($this->Rows);
|
|
|
|
} else {
|
2018-03-23 04:23:51 +01:00
|
|
|
$field = TextField::create($this->Name, $this->Title ?: false, $this->Default)
|
2017-08-14 02:29:57 +02:00
|
|
|
->setFieldHolderTemplate(EditableFormField::class . '_holder')
|
|
|
|
->setTemplate(EditableFormField::class);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$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) {
|
2019-05-17 04:55:49 +02:00
|
|
|
$field->setAttribute('data-rule-minlength', (int) $this->MinLength);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($this->MaxLength) && $this->MaxLength > 0) {
|
|
|
|
if ($field instanceof TextField) {
|
2019-05-17 04:55:49 +02:00
|
|
|
$field->setMaxLength((int) $this->MaxLength);
|
2016-05-16 08:05:05 +02:00
|
|
|
}
|
2019-05-17 04:55:49 +02:00
|
|
|
$field->setAttribute('data-rule-maxlength', (int) $this->MaxLength);
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2015-08-27 13:54:28 +02:00
|
|
|
|
2016-12-13 22:12:46 +01:00
|
|
|
if ($this->Autocomplete) {
|
|
|
|
$field->setAttribute('autocomplete', $this->Autocomplete);
|
|
|
|
}
|
2016-07-21 07:53:59 +02:00
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|