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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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