mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
ENHANCEMENT Ensure DBVarchar scaffolds text field with TextField with appropriate max length
Fixes #1413
This commit is contained in:
parent
f9b9e98279
commit
324bdad48c
@ -22,7 +22,9 @@ class TextField extends FormField
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @param null|string $title
|
* @param null|string $title
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @param null|int $maxLength
|
* @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.
|
||||||
* @param null|Form $form
|
* @param null|Form $form
|
||||||
*/
|
*/
|
||||||
public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null)
|
public function __construct($name, $title = null, $value = '', $maxLength = null, $form = null)
|
||||||
@ -40,8 +42,7 @@ class TextField extends FormField
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $maxLength
|
* @param int $maxLength
|
||||||
*
|
* @return $this
|
||||||
* @return static
|
|
||||||
*/
|
*/
|
||||||
public function setMaxLength($maxLength)
|
public function setMaxLength($maxLength)
|
||||||
{
|
{
|
||||||
|
@ -548,9 +548,7 @@ abstract class DBField extends ViewableData implements DBIndexable
|
|||||||
*/
|
*/
|
||||||
public function scaffoldFormField($title = null, $params = null)
|
public function scaffoldFormField($title = null, $params = null)
|
||||||
{
|
{
|
||||||
$field = new TextField($this->name, $title);
|
return TextField::create($this->name, $title);
|
||||||
|
|
||||||
return $field;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -124,12 +124,12 @@ class DBHTMLVarchar extends DBVarchar
|
|||||||
|
|
||||||
public function scaffoldFormField($title = null, $params = null)
|
public function scaffoldFormField($title = null, $params = null)
|
||||||
{
|
{
|
||||||
return new HTMLEditorField($this->name, $title, 1);
|
return HTMLEditorField::create($this->name, $title, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function scaffoldSearchField($title = null)
|
public function scaffoldSearchField($title = null)
|
||||||
{
|
{
|
||||||
return new TextField($this->name, $title);
|
return TextField::create($this->name, $title);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSchemaValue()
|
public function getSchemaValue()
|
||||||
|
@ -2,10 +2,11 @@
|
|||||||
|
|
||||||
namespace SilverStripe\ORM\FieldType;
|
namespace SilverStripe\ORM\FieldType;
|
||||||
|
|
||||||
use SilverStripe\ORM\DB;
|
|
||||||
use SilverStripe\Core\Config\Config;
|
use SilverStripe\Core\Config\Config;
|
||||||
use SilverStripe\Forms\TextField;
|
|
||||||
use SilverStripe\Forms\NullableField;
|
use SilverStripe\Forms\NullableField;
|
||||||
|
use SilverStripe\Forms\TextField;
|
||||||
|
use SilverStripe\ORM\Connect\MySQLDatabase;
|
||||||
|
use SilverStripe\ORM\DB;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Varchar represents a variable-length string of up to 255 characters, designed to store raw text
|
* Class Varchar represents a variable-length string of up to 255 characters, designed to store raw text
|
||||||
@ -22,6 +23,11 @@ class DBVarchar extends DBString
|
|||||||
"URL" => "Text",
|
"URL" => "Text",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Max size of this field
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
protected $size;
|
protected $size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,8 +64,8 @@ class DBVarchar extends DBString
|
|||||||
*/
|
*/
|
||||||
public function requireField()
|
public function requireField()
|
||||||
{
|
{
|
||||||
$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
|
$charset = Config::inst()->get(MySQLDatabase::class, 'charset');
|
||||||
$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
|
$collation = Config::inst()->get(MySQLDatabase::class, 'collation');
|
||||||
|
|
||||||
$parts = array(
|
$parts = array(
|
||||||
'datatype'=>'varchar',
|
'datatype'=>'varchar',
|
||||||
@ -117,12 +123,14 @@ class DBVarchar extends DBString
|
|||||||
|
|
||||||
public function scaffoldFormField($title = null, $params = null)
|
public function scaffoldFormField($title = null, $params = null)
|
||||||
{
|
{
|
||||||
if (!$this->nullifyEmpty) {
|
// Set field with appropriate size
|
||||||
// Allow the user to select if it's null instead of automatically assuming empty string is
|
$field = TextField::create($this->name, $title);
|
||||||
return new NullableField(new TextField($this->name, $title));
|
$field->setMaxLength($this->getSize());
|
||||||
} else {
|
|
||||||
// Automatically determine null (empty string)
|
// Allow the user to select if it's null instead of automatically assuming empty string is
|
||||||
return parent::scaffoldFormField($title);
|
if (!$this->getNullifyEmpty()) {
|
||||||
|
return NullableField::create($field);
|
||||||
}
|
}
|
||||||
|
return $field;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
30
tests/php/ORM/DBVarcharTest.php
Normal file
30
tests/php/ORM/DBVarcharTest.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\ORM\Tests;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\SapphireTest;
|
||||||
|
use SilverStripe\Forms\NullableField;
|
||||||
|
use SilverStripe\Forms\TextField;
|
||||||
|
|
||||||
|
class DBVarcharTest extends SapphireTest
|
||||||
|
{
|
||||||
|
protected static $extra_dataobjects = [
|
||||||
|
DBVarcharTest\TestObject::class,
|
||||||
|
];
|
||||||
|
|
||||||
|
public function testScaffold()
|
||||||
|
{
|
||||||
|
$obj = new DBVarcharTest\TestObject();
|
||||||
|
/** @var TextField $field */
|
||||||
|
$field = $obj->dbObject('Title')->scaffoldFormField();
|
||||||
|
$this->assertInstanceOf(TextField::class, $field);
|
||||||
|
$this->assertEquals(129, $field->getMaxLength());
|
||||||
|
|
||||||
|
/** @var NullableField $nullable */
|
||||||
|
$nullable = $obj->dbObject('NullableField')->scaffoldFormField();
|
||||||
|
$this->assertInstanceOf(NullableField::class, $nullable);
|
||||||
|
$innerField = $nullable->valueField;
|
||||||
|
$this->assertInstanceOf(TextField::class, $innerField);
|
||||||
|
$this->assertEquals(111, $innerField->getMaxLength());
|
||||||
|
}
|
||||||
|
}
|
16
tests/php/ORM/DBVarcharTest/TestObject.php
Normal file
16
tests/php/ORM/DBVarcharTest/TestObject.php
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace SilverStripe\ORM\Tests\DBVarcharTest;
|
||||||
|
|
||||||
|
use SilverStripe\Dev\TestOnly;
|
||||||
|
use SilverStripe\ORM\DataObject;
|
||||||
|
|
||||||
|
class TestObject extends DataObject implements TestOnly
|
||||||
|
{
|
||||||
|
private static $table_name = 'DBVarcharTest_TestObject';
|
||||||
|
|
||||||
|
private static $db = [
|
||||||
|
'Title' => 'Varchar(129)',
|
||||||
|
'NullableField' => 'Varchar(111, ["nullifyEmpty" => false])'
|
||||||
|
];
|
||||||
|
}
|
@ -106,7 +106,8 @@ class SearchContextTest extends SapphireTest
|
|||||||
$context = $company->getDefaultSearchContext();
|
$context = $company->getDefaultSearchContext();
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
new FieldList(
|
new FieldList(
|
||||||
new TextField("Name", 'Name'),
|
(new TextField("Name", 'Name'))
|
||||||
|
->setMaxLength(255),
|
||||||
new TextareaField("Industry", 'Industry'),
|
new TextareaField("Industry", 'Industry'),
|
||||||
new NumericField("AnnualProfit", 'The Almighty Annual Profit')
|
new NumericField("AnnualProfit", 'The Almighty Annual Profit')
|
||||||
),
|
),
|
||||||
|
Loading…
Reference in New Issue
Block a user