2008-09-29 03:18:23 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EditableTextField
|
2009-04-17 02:26:40 +00:00
|
|
|
*
|
|
|
|
* This control represents a user-defined text field in a user defined form
|
|
|
|
*
|
|
|
|
* @package userforms
|
2008-09-29 03:18:23 +00:00
|
|
|
*/
|
2009-12-07 02:04:20 +00:00
|
|
|
|
2008-09-29 03:18:23 +00:00
|
|
|
class EditableTextField extends EditableFormField {
|
2009-03-16 21:32:16 +00:00
|
|
|
|
2013-04-02 18:34:43 -07:00
|
|
|
private static $singular_name = 'Text Field';
|
2009-03-16 21:29:19 +00:00
|
|
|
|
2013-04-02 18:34:43 -07:00
|
|
|
private static $plural_name = 'Text Fields';
|
2015-07-24 14:37:48 +12:00
|
|
|
|
|
|
|
private static $db = array(
|
|
|
|
'MinLength' => 'Int',
|
|
|
|
'MaxLength' => 'Int',
|
|
|
|
'Rows' => 'Int(1)'
|
|
|
|
);
|
|
|
|
|
|
|
|
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'
|
|
|
|
))
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
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 03:18:23 +00:00
|
|
|
}
|
|
|
|
|
2010-09-03 05:06:13 +00:00
|
|
|
/**
|
|
|
|
* @return TextareaField|TextField
|
|
|
|
*/
|
2012-05-04 13:39:08 +12:00
|
|
|
public function getFormField() {
|
2015-07-24 14:37:48 +12:00
|
|
|
if($this->Rows > 1) {
|
2015-08-21 15:21:33 +12:00
|
|
|
$field = TextareaField::create($this->Name, $this->EscapedTitle, $this->Default)
|
|
|
|
->setFieldHolderTemplate('UserFormsField_holder')
|
|
|
|
->setTemplate('UserFormsTextareaField')
|
|
|
|
->setRows($this->Rows);
|
2015-07-24 14:37:48 +12:00
|
|
|
} else {
|
2015-08-21 15:21:33 +12:00
|
|
|
$field = TextField::create($this->Name, $this->EscapedTitle, $this->Default, $this->MaxLength)
|
|
|
|
->setFieldHolderTemplate('UserFormsField_holder')
|
|
|
|
->setTemplate('UserFormsField');
|
2008-09-29 03:18:23 +00:00
|
|
|
}
|
2015-08-21 15:21:33 +12:00
|
|
|
|
2015-08-10 17:03:36 +12:00
|
|
|
$this->doUpdateFormField($field);
|
2015-08-21 15:21:33 +12:00
|
|
|
|
2014-12-22 12:19:33 +10:30
|
|
|
return $field;
|
2008-09-29 03:18:23 +00:00
|
|
|
}
|
2015-08-11 11:40:37 +12:00
|
|
|
|
2009-04-21 03:44:13 +00:00
|
|
|
/**
|
2015-08-11 11:40:37 +12:00
|
|
|
* Updates a formfield with the additional metadata specified by this field
|
2009-04-21 03:44:13 +00:00
|
|
|
*
|
2015-08-11 11:40:37 +12:00
|
|
|
* @param FormField $field
|
2009-04-21 03:44:13 +00:00
|
|
|
*/
|
2015-08-11 11:40:37 +12:00
|
|
|
protected function updateFormField($field) {
|
|
|
|
parent::updateFormField($field);
|
|
|
|
|
2015-07-24 14:37:48 +12:00
|
|
|
if($this->MinLength) {
|
2015-08-11 11:40:37 +12:00
|
|
|
$field->setAttribute('data-rule-minlength', $this->MinLength);
|
|
|
|
}
|
|
|
|
|
2015-07-24 14:37:48 +12:00
|
|
|
if($this->MaxLength) {
|
2015-08-11 11:40:37 +12:00
|
|
|
$field->setAttribute('data-rule-maxlength', $this->MaxLength);
|
2014-04-16 11:48:10 +12:00
|
|
|
}
|
2009-04-21 03:44:13 +00:00
|
|
|
}
|
2012-07-17 16:09:31 +12:00
|
|
|
}
|