From 97dac7028c2b874349e0ff3c73285316983c82d0 Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Mon, 8 May 2017 17:21:51 +1200 Subject: [PATCH] De-couple schema type and type attribute --- src/Forms/DateField.php | 5 ++++- src/Forms/DatetimeField.php | 5 ++++- src/Forms/EmailField.php | 15 ++------------- src/Forms/FileField.php | 10 ++-------- src/Forms/FormField.php | 36 ++++++++++++++++++++++++++++++++++-- src/Forms/HiddenField.php | 15 ++------------- src/Forms/NumericField.php | 6 +++++- src/Forms/PasswordField.php | 6 +++--- src/Forms/TimeField.php | 6 ++++-- 9 files changed, 60 insertions(+), 44 deletions(-) diff --git a/src/Forms/DateField.php b/src/Forms/DateField.php index 9d4bed607..b111f08e3 100644 --- a/src/Forms/DateField.php +++ b/src/Forms/DateField.php @@ -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; diff --git a/src/Forms/DatetimeField.php b/src/Forms/DatetimeField.php index ed703621e..324b5fda3 100644 --- a/src/Forms/DatetimeField.php +++ b/src/Forms/DatetimeField.php @@ -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; diff --git a/src/Forms/EmailField.php b/src/Forms/EmailField.php index dcb62091e..be432e1e6 100644 --- a/src/Forms/EmailField.php +++ b/src/Forms/EmailField.php @@ -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. * diff --git a/src/Forms/FileField.php b/src/Forms/FileField.php index 30fd4bd77..084f74256 100644 --- a/src/Forms/FileField.php +++ b/src/Forms/FileField.php @@ -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 */ diff --git a/src/Forms/FormField.php b/src/Forms/FormField.php index 93e2e7314..62725ddb4 100644 --- a/src/Forms/FormField.php +++ b/src/Forms/FormField.php @@ -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(), diff --git a/src/Forms/HiddenField.php b/src/Forms/HiddenField.php index c4b18d6d9..5171bec51 100644 --- a/src/Forms/HiddenField.php +++ b/src/Forms/HiddenField.php @@ -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); diff --git a/src/Forms/NumericField.php b/src/Forms/NumericField.php index 147b3e37a..d4a59efa9 100644 --- a/src/Forms/NumericField.php +++ b/src/Forms/NumericField.php @@ -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; } diff --git a/src/Forms/PasswordField.php b/src/Forms/PasswordField.php index c7450e7f6..cedfe0374 100644 --- a/src/Forms/PasswordField.php +++ b/src/Forms/PasswordField.php @@ -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'); diff --git a/src/Forms/TimeField.php b/src/Forms/TimeField.php index 68c60ccca..14fc20ff8 100644 --- a/src/Forms/TimeField.php +++ b/src/Forms/TimeField.php @@ -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;