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';
|
2009-03-16 22:29:19 +01: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',
|
|
|
|
'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 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) {
|
2014-12-22 02:49:33 +01:00
|
|
|
$field = TextareaField::create($this->Name, $this->Title);
|
2015-07-24 04:37:48 +02:00
|
|
|
$field->setRows($this->Rows);
|
|
|
|
} else {
|
|
|
|
$field = TextField::create($this->Name, $this->Title, null, $this->MaxLength);
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
2014-12-22 02:49:33 +01:00
|
|
|
|
|
|
|
if ($this->Required) {
|
|
|
|
// Required validation can conflict so add the Required validation messages
|
|
|
|
// as input attributes
|
|
|
|
$errorMessage = $this->getErrorMessage()->HTML();
|
|
|
|
$field->setAttribute('data-rule-required', 'true');
|
|
|
|
$field->setAttribute('data-msg-required', $errorMessage);
|
|
|
|
}
|
2015-07-24 04:37:48 +02:00
|
|
|
|
|
|
|
$field->setValue($this->Default);
|
2014-12-22 02:49:33 +01:00
|
|
|
|
|
|
|
return $field;
|
2008-09-29 05:18:23 +02:00
|
|
|
}
|
2009-04-21 05:44:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the validation information related to this field. This is
|
|
|
|
* interrupted as a JSON object for validate plugin and used in the
|
|
|
|
* PHP.
|
|
|
|
*
|
|
|
|
* @see http://docs.jquery.com/Plugins/Validation/Methods
|
2012-04-14 08:36:50 +02:00
|
|
|
* @return array
|
2009-04-21 05:44:13 +02:00
|
|
|
*/
|
|
|
|
public function getValidation() {
|
2014-04-16 01:48:10 +02:00
|
|
|
$options = parent::getValidation();
|
2015-07-24 04:37:48 +02:00
|
|
|
if($this->MinLength) {
|
|
|
|
$options['minlength'] = (int)$this->MinLength;
|
|
|
|
}
|
|
|
|
if($this->MaxLength) {
|
|
|
|
$options['maxlength'] = (int)$this->MaxLength;
|
2014-04-16 01:48:10 +02:00
|
|
|
}
|
2009-04-21 05:44:13 +02:00
|
|
|
return $options;
|
|
|
|
}
|
2012-07-17 06:09:31 +02:00
|
|
|
}
|