From 97dac7028c2b874349e0ff3c73285316983c82d0 Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Mon, 8 May 2017 17:21:51 +1200 Subject: [PATCH 1/2] 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; From fd51f35bc295e5e41b434b2e8a6036779c02350b Mon Sep 17 00:00:00 2001 From: Saophalkun Ponlu Date: Tue, 9 May 2017 16:42:08 +1200 Subject: [PATCH 2/2] Update tests --- src/Forms/FormAction.php | 11 ++++++---- src/Forms/PopoverField.php | 2 ++ .../FormSchemaTest/testGetNestedSchema.json | 21 ++++++++++++------- .../Forms/FormSchemaTest/testGetSchema.json | 5 +++-- .../FormSchemaTest/testSchemaValidation.json | 17 +++++++++------ 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/Forms/FormAction.php b/src/Forms/FormAction.php index 0cc5985d8..99a69b87b 100644 --- a/src/Forms/FormAction.php +++ b/src/Forms/FormAction.php @@ -173,20 +173,23 @@ class FormAction extends FormField return 'action'; } - public function getAttributes() + public function getInputType() { if (isset($this->attributes['type'])) { - $type = $this->attributes['type']; + return $this->attributes['type']; } else { - $type = (isset($this->attributes['src'])) ? 'image' : 'submit'; + return (isset($this->attributes['src'])) ? 'image' : 'submit'; } + } + public function getAttributes() + { return array_merge( parent::getAttributes(), array( 'disabled' => ($this->isReadonly() || $this->isDisabled()), 'value' => $this->Title(), - 'type' => $type, + 'type' => $this->getInputType(), 'title' => ($this->useButtonTag) ? $this->description : null, ) ); diff --git a/src/Forms/PopoverField.php b/src/Forms/PopoverField.php index a5bab03ab..0ec7589b7 100644 --- a/src/Forms/PopoverField.php +++ b/src/Forms/PopoverField.php @@ -30,6 +30,8 @@ class PopoverField extends FieldGroup */ protected $popoverTitle = null; + protected $inputType = null; + /** * Placement of the popup box, relative to the element triggering it. * Valid values: bottom, top, left, right. diff --git a/tests/php/Forms/FormSchemaTest/testGetNestedSchema.json b/tests/php/Forms/FormSchemaTest/testGetNestedSchema.json index e56722526..dc060b0d5 100644 --- a/tests/php/Forms/FormSchemaTest/testGetNestedSchema.json +++ b/tests/php/Forms/FormSchemaTest/testGetNestedSchema.json @@ -16,7 +16,8 @@ { "id": "Form_TestForm_Name", "name": "Name", - "type": "Text", + "type": "text", + "schemaType": "Text", "component": null, "holderId": "Form_TestForm_Name_Holder", "title": "Name", @@ -35,7 +36,8 @@ { "id": "Form_TestForm_SecurityID", "name": "SecurityID", - "type": "Hidden", + "type": "hidden", + "schemaType": "Hidden", "component": null, "holderId": "Form_TestForm_SecurityID_Holder", "title": "Security ID", @@ -57,7 +59,8 @@ "id": "Form_TestForm_action_save", "title": "Save", "name": "action_save", - "type": null, + "type": "submit", + "schemaType": null, "component": "FormAction", "holderId": "Form_TestForm_action_save_Holder", "source": null, @@ -80,7 +83,8 @@ "id": "Form_TestForm_action_cancel", "title": "Cancel", "name": "action_cancel", - "type": null, + "type": "submit", + "schemaType": null, "component": "FormAction", "holderId": "Form_TestForm_action_cancel_Holder", "source": null, @@ -103,7 +107,8 @@ "id": "Form_TestForm_Moreoptions", "title": "More options", "name": "Moreoptions", - "type": "Structural", + "schemaType": "Structural", + "type": null, "component": "PopoverField", "holderId": "Form_TestForm_Moreoptions_Holder", "source": null, @@ -133,7 +138,8 @@ "id": "Form_TestForm_action_publish", "title": "Publish record", "name": "action_publish", - "type": null, + "type": "submit", + "schemaType": null, "component": "FormAction", "holderId": "Form_TestForm_action_publish_Holder", "source": null, @@ -156,7 +162,8 @@ "id": "Form_TestForm_action_archive", "title": "Archive", "name": "action_archive", - "type": null, + "type": "submit", + "schemaType": null, "component": "FormAction", "holderId": "Form_TestForm_action_archive_Holder", "source": null, diff --git a/tests/php/Forms/FormSchemaTest/testGetSchema.json b/tests/php/Forms/FormSchemaTest/testGetSchema.json index 47d31ad56..fed186457 100644 --- a/tests/php/Forms/FormSchemaTest/testGetSchema.json +++ b/tests/php/Forms/FormSchemaTest/testGetSchema.json @@ -16,7 +16,8 @@ { "id": "Form_TestForm_SecurityID", "name": "SecurityID", - "type": "Hidden", + "type": "hidden", + "schemaType": "Hidden", "component": null, "holderId": "Form_TestForm_SecurityID_Holder", "title": "Security ID", @@ -34,4 +35,4 @@ } ], "actions": [] -} \ No newline at end of file +} diff --git a/tests/php/Forms/FormSchemaTest/testSchemaValidation.json b/tests/php/Forms/FormSchemaTest/testSchemaValidation.json index 3720c3342..69d4c5476 100644 --- a/tests/php/Forms/FormSchemaTest/testSchemaValidation.json +++ b/tests/php/Forms/FormSchemaTest/testSchemaValidation.json @@ -16,7 +16,8 @@ { "name": "Name", "id": "Form_TestForm_Name", - "type": "Text", + "type": "text", + "schemaType": "Text", "component": null, "holderId": "Form_TestForm_Name_Holder", "title": "Name", @@ -40,7 +41,8 @@ { "name": "Date", "id": "Form_TestForm_Date", - "type": "Date", + "type": "date", + "schemaType": "Date", "component": null, "holderId": "Form_TestForm_Date_Holder", "title": "Date", @@ -66,7 +68,8 @@ { "name": "Number", "id": "Form_TestForm_Number", - "type": "Decimal", + "type": "number", + "schemaType": "Decimal", "component": null, "holderId": "Form_TestForm_Number_Holder", "title": "Number", @@ -87,7 +90,8 @@ { "name": "Money", "id": "Form_TestForm_Money", - "type": "Text", + "type": "text", + "schemaType": "Text", "component": null, "holderId": "Form_TestForm_Money_Holder", "title": "Money", @@ -108,7 +112,8 @@ { "name": "SecurityID", "id": "Form_TestForm_SecurityID", - "type": "Hidden", + "type": "hidden", + "schemaType": "Hidden", "component": null, "holderId": "Form_TestForm_SecurityID_Holder", "title": "Security ID", @@ -126,4 +131,4 @@ } ], "actions": [] -} \ No newline at end of file +}