silverstripe-framework/forms/TextField.php
Damian Mooyman 8331171f2c Merge remote-tracking branch 'origin/3.1' into 3
Conflicts:
	.scrutinizer.yml
	admin/javascript/LeftAndMain.Panel.js
	core/startup/ParameterConfirmationToken.php
	dev/Debug.php
	dev/FixtureBlueprint.php
	docs/en/00_Getting_Started/05_Coding_Conventions.md
	docs/en/00_Getting_Started/index.md
	docs/en/02_Developer_Guides/01_Templates/01_Syntax.md
	filesystem/File.php
	filesystem/Folder.php
	forms/FieldList.php
	forms/LabelField.php
	forms/MoneyField.php
	forms/TextField.php
	forms/TreeDropdownField.php
	forms/Validator.php
	forms/gridfield/GridField.php
	forms/gridfield/GridFieldExportButton.php
	lang/de.yml
	lang/fi.yml
	model/DataObject.php
	model/SQLQuery.php
	parsers/ShortcodeParser.php
	security/ChangePasswordForm.php
	security/Security.php
	tests/control/DirectorTest.php
	tests/core/startup/ParameterConfirmationTokenTest.php
	tests/dev/FixtureBlueprintTest.php
	tests/forms/FieldListTest.php
	tests/forms/MoneyFieldTest.php
	tests/model/SQLQueryTest.php
	tests/security/SecurityTest.php
2015-06-02 19:13:38 +12:00

107 lines
1.8 KiB
PHP

<?php
/**
* Text input field.
*
* @package forms
* @subpackage fields-basic
*/
class TextField extends FormField {
/**
* @var int
*/
protected $maxLength;
/**
* Returns an input field.
*
* @param string $name
* @param null|string $title
* @param string $value
* @param null|int $maxLength
* @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
*
* @return static
*/
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() {
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(
'TextField.VALIDATEMAXLENGTH',
'The value for {name} must not exceed {maxLength} characters in length',
array('name' => $this->getName(), 'maxLength' => $this->maxLength)
),
"validation"
);
return false;
}
return true;
}
}