2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2015-04-27 01:59:06 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms;
|
|
|
|
|
|
|
|
use SilverStripe\Dev\Deprecation;
|
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Text input field.
|
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class TextField extends FormField
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $maxLength;
|
|
|
|
|
|
|
|
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_TEXT;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an input field.
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
* @param null|string $title
|
|
|
|
* @param string $value
|
2017-10-24 22:30:04 +02:00
|
|
|
* @param null|int $maxLength Max characters to allow for this field. If this value is stored
|
|
|
|
* against a DB field with a fixed size it's recommended to set an appropriate max length
|
|
|
|
* matching this size.
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param null|Form $form
|
|
|
|
*/
|
|
|
|
public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null)
|
|
|
|
{
|
|
|
|
if ($maxLength) {
|
|
|
|
$this->setMaxLength($maxLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($form) {
|
|
|
|
$this->setForm($form);
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::__construct($name, $title, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $maxLength
|
2017-10-24 22:30:04 +02:00
|
|
|
* @return $this
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function setMaxLength($maxLength)
|
|
|
|
{
|
|
|
|
$this->maxLength = $maxLength;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return null|int
|
|
|
|
*/
|
|
|
|
public function getMaxLength()
|
|
|
|
{
|
|
|
|
return $this->maxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAttributes()
|
|
|
|
{
|
|
|
|
$maxLength = $this->getMaxLength();
|
|
|
|
|
|
|
|
$attributes = array();
|
|
|
|
|
|
|
|
if ($maxLength) {
|
|
|
|
$attributes['maxLength'] = $maxLength;
|
|
|
|
$attributes['size'] = min($maxLength, 30);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_merge(
|
|
|
|
parent::getAttributes(),
|
|
|
|
$attributes
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function InternallyLabelledField()
|
|
|
|
{
|
|
|
|
Deprecation::notice('4.0', 'Please use ->setValue() instead');
|
|
|
|
|
|
|
|
if (!$this->value) {
|
|
|
|
$this->value = $this->Title();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->Field();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate this field
|
|
|
|
*
|
|
|
|
* @param Validator $validator
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function validate($validator)
|
|
|
|
{
|
|
|
|
if (!is_null($this->maxLength) && mb_strlen($this->value) > $this->maxLength) {
|
|
|
|
$validator->validationError(
|
|
|
|
$this->name,
|
|
|
|
_t(
|
2017-04-20 03:15:24 +02:00
|
|
|
'SilverStripe\\Forms\\TextField.VALIDATEMAXLENGTH',
|
2016-11-29 00:31:16 +01:00
|
|
|
'The value for {name} must not exceed {maxLength} characters in length',
|
|
|
|
array('name' => $this->getName(), 'maxLength' => $this->maxLength)
|
|
|
|
),
|
|
|
|
"validation"
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSchemaValidation()
|
|
|
|
{
|
|
|
|
$rules = parent::getSchemaValidation();
|
|
|
|
if ($this->getMaxLength()) {
|
|
|
|
$rules['max'] = [
|
|
|
|
'length' => $this->getMaxLength(),
|
2016-11-22 03:18:26 +01:00
|
|
|
];
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
return $rules;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|