De-couple schema type and type attribute

This commit is contained in:
Saophalkun Ponlu 2017-05-08 17:21:51 +12:00
parent 6cd9bec766
commit 97dac7028c
9 changed files with 60 additions and 44 deletions

View File

@ -94,6 +94,8 @@ class DateField extends TextField
*/
protected $clientLocale = null;
protected $inputType = 'date';
/**
* Min date
*
@ -283,9 +285,10 @@ class DateField extends TextField
$attributes['lang'] = i18n::convert_rfc1766($this->getLocale());
if ($this->getHTML5()) {
$attributes['type'] = 'date';
$attributes['min'] = $this->getMinDate();
$attributes['max'] = $this->getMaxDate();
} else {
$attributes['type'] = 'text';
}
return $attributes;

View File

@ -30,6 +30,8 @@ class DatetimeField extends TextField
*/
protected $locale = null;
protected $inputType = 'datetime-local';
/**
* Min date time
*
@ -94,9 +96,10 @@ class DatetimeField extends TextField
$attributes['lang'] = i18n::convert_rfc1766($this->getLocale());
if ($this->getHTML5()) {
$attributes['type'] = 'datetime-local';
$attributes['min'] = $this->internalToFrontend($this->getMinDatetime());
$attributes['max'] = $this->internalToFrontend($this->getMaxDatetime());
} else {
$attributes['type'] = 'text';
}
return $attributes;

View File

@ -7,6 +7,8 @@ namespace SilverStripe\Forms;
*/
class EmailField extends TextField
{
protected $inputType = 'email';
/**
* {@inheritdoc}
*/
@ -15,19 +17,6 @@ class EmailField extends TextField
return 'email text';
}
/**
* {@inheritdoc}
*/
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
array(
'type' => 'email',
)
);
}
/**
* Validates for RFC 2822 compliant email addresses.
*

View File

@ -47,6 +47,8 @@ class FileField extends FormField implements FileHandleField
{
use UploadReceiver;
protected $inputType = 'file';
/**
* Flag to automatically determine and save a has_one-relationship
* on the saved record (e.g. a "Player" has_one "PlayerImage" would
@ -83,14 +85,6 @@ class FileField extends FormField implements FileHandleField
return parent::Field($properties);
}
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
array('type' => 'file')
);
}
/**
* @param DataObject|DataObjectInterface $record
*/

View File

@ -89,6 +89,13 @@ class FormField extends RequestHandler
*/
protected $form;
/**
* This is INPUT's type attribute value.
*
* @var string
*/
protected $inputType = 'text';
/**
* @var string
*/
@ -446,6 +453,16 @@ class FormField extends RequestHandler
return $this->name;
}
/**
* Returns the field input name.
*
* @return string
*/
public function getInputType()
{
return $this->inputType;
}
/**
* Returns the field value.
*
@ -678,7 +695,7 @@ class FormField extends RequestHandler
public function getAttributes()
{
$attributes = array(
'type' => 'text',
'type' => $this->getInputType(),
'name' => $this->getName(),
'value' => $this->Value(),
'class' => $this->extraClass(),
@ -811,6 +828,20 @@ class FormField extends RequestHandler
return $this;
}
/**
* Set the field input type.
*
* @param string $type
*
* @return $this
*/
public function setInputType($type)
{
$this->inputType = $type;
return $this;
}
/**
* Set the container form.
*
@ -1499,7 +1530,8 @@ class FormField extends RequestHandler
return [
'name' => $this->getName(),
'id' => $this->ID(),
'type' => $this->getSchemaDataType(),
'type' => $this->getInputType(),
'schemaType' => $this->getSchemaDataType(),
'component' => $this->getSchemaComponent(),
'holderId' => $this->HolderID(),
'title' => $this->Title(),

View File

@ -10,6 +10,8 @@ class HiddenField extends FormField
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_HIDDEN;
protected $inputType = 'hidden';
/**
* @param array $properties
* @return string
@ -39,19 +41,6 @@ class HiddenField extends FormField
return true;
}
/**
* {@inheritdoc}
*/
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
array(
'type' => 'hidden',
)
);
}
function SmallFieldHolder($properties = array())
{
return $this->FieldHolder($properties);

View File

@ -15,6 +15,8 @@ class NumericField extends TextField
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_DECIMAL;
protected $inputType = 'number';
/**
* Used to determine if the number given is in the correct format when validating
*
@ -170,9 +172,11 @@ class NumericField extends TextField
{
$attributes = parent::getAttributes();
if ($this->getHTML5()) {
$attributes['type'] = 'number';
$attributes['step'] = $this->getStep();
} else {
$attributes['type'] = 'text';
}
return $attributes;
}

View File

@ -17,6 +17,8 @@ class PasswordField extends TextField
*/
private static $autocomplete;
protected $inputType = 'password';
/**
* Returns an input field.
*
@ -42,9 +44,7 @@ class PasswordField extends TextField
*/
public function getAttributes()
{
$attributes = array(
'type' => 'password',
);
$attributes = array();
$autocomplete = $this->config()->get('autocomplete');

View File

@ -28,6 +28,8 @@ class TimeField extends TextField
*/
protected $locale = null;
protected $inputType = 'time';
/**
* Override time format. If empty will default to that used by the current locale.
*
@ -224,8 +226,8 @@ class TimeField extends TextField
{
$attributes = parent::getAttributes();
if ($this->getHTML5()) {
$attributes['type'] = 'time';
if (!$this->getHTML5()) {
$attributes['type'] = 'text';
}
return $attributes;