silverstripe-framework/src/Forms/TextareaField.php

180 lines
3.3 KiB
PHP
Raw Normal View History

<?php
2015-04-27 05:15:32 +02:00
namespace SilverStripe\Forms;
/**
* TextareaField creates a multi-line text field,
* allowing more data to be entered than a standard
* text field. It creates the `<textarea>` tag in the
* form HTML.
2014-08-15 08:53:05 +02:00
*
* <code>
* new TextareaField(
* $name = "description",
* $title = "Description",
* $value = "This is the default description"
* );
* </code>
*/
2016-11-29 00:31:16 +01:00
class TextareaField extends FormField
{
/**
* Value should be XML
*
* @var array
*/
private static $casting = [
2016-11-29 00:31:16 +01:00
'Value' => 'Text',
'ValueEntities' => 'HTMLFragment([\'shortcodes\' => false])',
];
2017-12-04 04:40:40 +01:00
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT;
2017-12-04 04:40:40 +01:00
/**
2016-11-29 00:31:16 +01:00
* Visible number of text lines.
*
* @var int
*/
protected $rows = 5;
/**
* Visible number of text columns.
*
* @var int
*/
protected $cols = 20;
2017-12-04 04:40:40 +01:00
/**
* @var int
*/
protected $maxLength;
2016-11-29 00:31:16 +01:00
/**
* Set textarea specific schema data
*/
public function getSchemaDataDefaults()
{
$data = parent::getSchemaDataDefaults();
$data['data']['rows'] = $this->getRows();
$data['data']['columns'] = $this->getColumns();
2017-12-04 04:40:40 +01:00
$data['data']['maxlength'] = $this->getMaxLength();
return $data;
}
2017-12-04 04:40:40 +01:00
/**
* Set the number of rows in the textarea
2016-11-29 00:31:16 +01:00
*
* @param int $rows
*
* @return $this
*/
public function setRows($rows)
{
$this->rows = $rows;
return $this;
}
/**
* Gets number of rows
*
* @return int
*/
public function getRows()
{
return $this->rows;
}
/**
* Set the number of columns in the textarea
*
* @param int $cols
*
* @return $this
*/
public function setColumns($cols)
{
$this->cols = $cols;
return $this;
}
/**
* Gets the number of columns in this textarea
*
* @return int
*/
public function getColumns()
{
return $this->cols;
}
2017-12-04 04:40:40 +01:00
/**
* @param int $maxLength
* @return $this
*/
public function setMaxLength($maxLength)
{
$this->maxLength = $maxLength;
return $this;
}
/**
* @return null|int
*/
public function getMaxLength()
{
return $this->maxLength;
}
2016-11-29 00:31:16 +01:00
/**
* {@inheritdoc}
*/
public function getAttributes()
{
2017-12-07 21:58:52 +01:00
$attributes = array_merge(
2016-11-29 00:31:16 +01:00
parent::getAttributes(),
[
2016-11-29 00:31:16 +01:00
'rows' => $this->getRows(),
'cols' => $this->getColumns(),
'value' => null,
2017-12-04 04:40:40 +01:00
'type' => null,
]
2016-11-29 00:31:16 +01:00
);
2017-12-07 21:58:52 +01:00
$maxLength = $this->getMaxLength();
2017-12-10 21:00:04 +01:00
if ($maxLength) {
2017-12-07 21:58:52 +01:00
$attributes['maxlength'] = $maxLength;
}
return $attributes;
2016-11-29 00:31:16 +01:00
}
/**
* {@inheritdoc}
*/
public function Type()
{
$parent = parent::Type();
if ($this->readonly) {
return $parent . ' readonly';
}
return $parent;
}
/**
* Return value with all values encoded in html entities
*
* @return string Raw HTML
*/
public function ValueEntities()
{
return htmlentities($this->Value(), ENT_COMPAT, 'UTF-8');
}
}