API Removed legacy form fields (fixes #6099)

This commit is contained in:
Ingo Schommer 2017-05-08 20:13:38 +12:00 committed by Damian Mooyman
parent afd1575267
commit 0d9b383631
12 changed files with 4 additions and 643 deletions

View File

@ -290,8 +290,6 @@ mappings:
CheckboxSetField: SilverStripe\Forms\CheckboxSetField
CompositeField: SilverStripe\Forms\CompositeField
ConfirmedPasswordField: SilverStripe\Forms\ConfirmedPasswordField
CountryDropdownField: SilverStripe\Forms\CountryDropdownField
CreditCardField: SilverStripe\Forms\CreditCardField
CurrencyField: SilverStripe\Forms\CurrencyField
CurrencyField_Readonly: SilverStripe\Forms\CurrencyField_Readonly
CurrencyField_Disabled: SilverStripe\Forms\CurrencyField_Disabled
@ -317,13 +315,10 @@ mappings:
GroupedDropdownField: SilverStripe\Forms\GroupedDropdownField
HeaderField: SilverStripe\Forms\HeaderField
HiddenField: SilverStripe\Forms\HiddenField
InlineFormAction: SilverStripe\Forms\InlineFormAction
InlineFormAction_ReadOnly: SilverStripe\Forms\InlineFormAction_ReadOnly
LabelField: SilverStripe\Forms\LabelField
ListboxField: SilverStripe\Forms\ListboxField
LiteralField: SilverStripe\Forms\LiteralField
LookupField: SilverStripe\Forms\LookupField
MemberDatetimeOptionsetField: SilverStripe\Forms\MemberDatetimeOptionsetField
MoneyField: SilverStripe\Forms\MoneyField
MultiSelectField: SilverStripe\Forms\MultiSelectField
NullableField: SilverStripe\Forms\NullableField
@ -331,7 +326,6 @@ mappings:
NumericField_Readonly: SilverStripe\Forms\NumericField_Readonly
OptionsetField: SilverStripe\Forms\OptionsetField
PasswordField: SilverStripe\Forms\PasswordField
PhoneNumberField: SilverStripe\Forms\PhoneNumberField
PopoverField: SilverStripe\Forms\PopoverField
PrintableTransformation: SilverStripe\Forms\PrintableTransformation
PrintableTransformation_TabSet: SilverStripe\Forms\PrintableTransformation_TabSet
@ -636,7 +630,6 @@ mappings:
GridFieldTest_HTMLFragments: SilverStripe\Forms\Tests\GridField\GridFieldTest\HTMLFragments
GridFieldTest_Permissions: SilverStripe\Forms\Tests\GridField\GridFieldTest\Permissions
GroupedDropdownFieldTest: SilverStripe\Forms\Tests\GroupedDropdownFieldTest
InlineFormActionTest: SilverStripe\Forms\Tests\InlineFormActionTest
ListboxFieldTest: SilverStripe\Forms\Tests\ListboxFieldTest
ListboxFieldTest_DataObject: SilverStripe\Forms\Tests\ListboxFieldTest\TestObject
ListboxFieldTest_Article: SilverStripe\Forms\Tests\ListboxFieldTest\Article

View File

@ -22,10 +22,7 @@ on the SilverStripe API documentation.
## Formatted input
* [api:AjaxUniqueTextField]: Text field that automatically checks that the value entered is unique for the given set of fields in a given set of tables.
* [api:ConfirmedPasswordField]: Two masked input fields, checks for matching passwords.
* [api:CountryDropdownField]: A simple extension to dropdown field, pre-configured to list countries.
* [api:CreditCardField]: Allows input of credit card numbers via four separate form fields, including generic validation of its numeric values.
* [api:CurrencyField]: Text field, validating its input as a currency. Limited to US-centric formats, including a hardcoded currency symbol and decimal separators.
See [api:MoneyField] for a more flexible implementation.
* [api:DateField]: Represents a date in a single input field, or separated into day, month, and year. Can optionally use a calendar popup.
@ -36,7 +33,6 @@ on the SilverStripe API documentation.
* [api:MoneyField]: A form field that can save into a [api:Money] database field.
* [api:NumericField]: Text input field with validation for numeric values.
* [api:OptionsetField]: Set of radio buttons designed to emulate a dropdown.
* [api:PhoneNumberField]: Field for displaying phone numbers. It separates the number, the area code and optionally the country code and extension.
* [api:SelectionGroup]: SelectionGroup represents a number of fields which are selectable by a radio button that appears at the beginning of each item.
* [api:TimeField]: Input field with time-specific, localised validation.

View File

@ -1283,6 +1283,9 @@ After (`mysite/_config/config.yml`):
- `getModules`
- `getConfigDirs`
- `getConfigs`
* Removed `CreditCardField`, `CountryDropdownField`, `PhoneNumberField`, `MemberDatetimeOptionsetField`, `InlineFormAction`.
Use custom code instead
* Removed `ResetFormAction`, use `FormAction::create()->setAttribute('type', 'reset')` instead
#### <a name="overview-general-deprecated"></a>General and Core Deprecated API
@ -1816,4 +1819,4 @@ New `TimeField` methods replace `getConfig()` / `setConfig()`
you will need to define this method and return a short name describing the login method.
* `MemberLoginForm` has a new constructor argument for the authenticator class, athough tis is usually
constructed by `MemberAuthenticator` and won't affect normal use.
* `Authenticator` methods `register` and `unregister` are deprecated in favor of using `Config`
* `Authenticator` methods `register` and `unregister` are deprecated in favor of using `Config`

View File

@ -53,7 +53,6 @@ en:
BAD_METHOD: 'This form requires a {method} submission'
CSRF_EXPIRED_MESSAGE: 'Your session has expired. Please re-submit the form.'
CSRF_FAILED_MESSAGE: 'There seems to have been a technical problem. Please click the back button, refresh your browser, and try again.'
VALIDATIONCREDIT: 'Please ensure you have entered the credit card number correctly'
VALIDATIONPASSWORDSDONTMATCH: 'Passwords don''t match'
VALIDATIONPASSWORDSNOTEMPTY: 'Passwords can''t be empty'
VALIDATIONSTRONGPASSWORD: 'Passwords must have at least one digit and one alphanumeric character'
@ -161,8 +160,6 @@ en:
IsNullLabel: 'Is Null'
SilverStripe\Forms\NumericField:
VALIDATION: '''{value}'' is not a number, only numbers can be accepted for this field'
SilverStripe\Forms\PhoneNumberField:
VALIDATION: 'Please enter a valid phone number'
SilverStripe\Forms\TimeField:
VALIDATEFORMAT: 'Please enter a valid time format ({format})'
SilverStripe\ORM\DataObject:

View File

@ -1,82 +0,0 @@
<?php
namespace SilverStripe\Forms;
use SilverStripe\i18n\i18n;
use SilverStripe\Security\Member;
/**
* A simple extension to dropdown field, pre-configured to list countries.
* It will default to the country of the current visitor.
*/
class CountryDropdownField extends DropdownField
{
/**
* Should we default the dropdown to the region determined from the user's locale?
*
* @config
* @var bool
*/
private static $default_to_locale = true;
/**
* The region code to default to if default_to_locale is set to false, or we can't
* determine a region from a locale.
*
* @config
* @var string
*/
private static $default_country = 'NZ';
protected $extraClasses = array('dropdown');
protected $schemaDataType = FormField::SCHEMA_DATA_TYPE_SINGLESELECT;
/**
* Get the locale of the Member, or if we're not logged in or don't have a locale, use the default one
* @return string
*/
protected function locale()
{
if (($member = Member::currentUser()) && $member->Locale) {
return $member->Locale;
}
return i18n::get_locale();
}
public function setSource($source)
{
if ($source) {
return parent::setSource($source);
}
// Get sorted countries
$source = i18n::getData()->getCountries();
return parent::setSource($source);
}
public function Field($properties = array())
{
$source = $this->getSource();
// Default value to best availabel locale
$value = $this->Value();
if (CountryDropdownField::config()->default_to_locale
&& (!$value || !isset($source[$value]))
&& $this->locale()
) {
$value = i18n::getData()->countryFromLocale(i18n::get_locale());
if ($value) {
$this->setValue($value);
}
}
// Default to default country otherwise
if (!$value || !isset($source[$value])) {
$this->setValue(CountryDropdownField::config()->default_country);
}
return parent::Field($properties);
}
}

View File

@ -1,121 +0,0 @@
<?php
namespace SilverStripe\Forms;
/**
* Allows input of credit card numbers via four separate form fields,
* including generic validation of its numeric values.
*
* @todo Validate
*/
class CreditCardField extends TextField
{
/**
* Add default attributes for use on all inputs.
*
* @return array List of attributes
*/
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
array(
'autocomplete' => 'off',
'maxlength' => 4,
'size' => 4
)
);
}
public function Field($properties = array())
{
$parts = $this->arrayValue();
$properties['ValueOne'] = $parts[0];
$properties['ValueTwo'] = $parts[1];
$properties['ValueThree'] = $parts[2];
$properties['ValueFour'] = $parts[3];
return parent::Field($properties);
}
/**
* Get tabindex HTML string
*
* @param int $increment Increase current tabindex by this value
* @return string
*/
public function getTabIndexHTML($increment = 0)
{
// we can't add a tabindex if there hasn't been one set yet.
if ($this->getAttribute('tabindex') === null) {
return false;
}
$tabIndex = (int)$this->getAttribute('tabindex') + (int)$increment;
return (is_numeric($tabIndex)) ? ' tabindex = "' . $tabIndex . '"' : '';
}
public function dataValue()
{
if (is_array($this->value)) {
return implode("", $this->value);
} else {
return $this->value;
}
}
/**
* Get either list of values, or null
*
* @return array
*/
public function arrayValue()
{
if (is_array($this->value)) {
return $this->value;
}
$value = $this->dataValue();
return $this->parseCreditCard($value);
}
/**
* Parse credit card value into list of four four-digit values
*
* @param string $value
* @return array|null
*/
protected function parseCreditCard($value)
{
if (preg_match("/([0-9]{4})([0-9]{4})([0-9]{4})([0-9]{4})/", $value, $parts)) {
return [ $parts[1], $parts[2], $parts[3], $parts[4] ];
}
return null;
}
public function validate($validator)
{
$value = $this->dataValue();
if (empty($value)) {
return true;
}
// Check if format is valid
if ($this->parseCreditCard($value)) {
return true;
}
// Format is invalid
$validator->validationError(
$this->name,
_t(
'SilverStripe\\Forms\\Form.VALIDATIONCREDIT',
"Please ensure you have entered the credit card number correctly"
),
"validation"
);
return false;
}
}

View File

@ -1,55 +0,0 @@
<?php
namespace SilverStripe\Forms;
use SilverStripe\View\Requirements;
/**
* Render a button that will submit the form its contained in through ajax.
*
* Caution: The form field does not include any JavaScript or CSS when used outside of the CMS context,
* since the required frontend dependencies are included through CMS bundling.
*
* @see framework/client/dist/js/InlineFormAction.js
*/
class InlineFormAction extends FormField
{
/**
* Create a new action button.
*
* @param string $action The method to call when the button is clicked
* @param string $title The label on the button
* @param string $extraClass A CSS class to apply to the button in addition to 'action'
*/
public function __construct($action, $title = "", $extraClass = '')
{
$this->extraClass = ' '.$extraClass;
parent::__construct($action, $title);
}
public function performReadonlyTransformation()
{
return $this->castedCopy('SilverStripe\\Forms\\InlineFormAction_ReadOnly');
}
/**
* @param array $properties
* @return string
*/
public function Field($properties = array())
{
return FormField::create_tag('input', array(
'type' => 'submit',
'name' => sprintf('action_%s', $this->getName()),
'value' => $this->title,
'id' => $this->ID(),
'class' => sprintf('action%s', $this->extraClass),
));
}
public function Title()
{
return false;
}
}

View File

@ -1,33 +0,0 @@
<?php
namespace SilverStripe\Forms;
/**
* Readonly version of {@link InlineFormAction}.
*/
class InlineFormAction_ReadOnly extends FormField
{
protected $readonly = true;
/**
* @param array $properties
* @return string
*/
public function Field($properties = array())
{
return FormField::create_tag('input', array(
'type' => 'submit',
'name' => sprintf('action_%s', $this->name),
'value' => $this->title,
'id' => $this->ID(),
'disabled' => 'disabled',
'class' => 'action disabled ' . $this->extraClass,
));
}
public function Title()
{
return false;
}
}

View File

@ -1,279 +0,0 @@
<?php
namespace SilverStripe\Forms;
use SilverStripe\ORM\DataObjectInterface;
/**
* Field for displaying phone numbers. It separates the number, the area code and optionally the country code
* and extension.
*/
class PhoneNumberField extends FieldGroup
{
/**
* Default area code
*
* @var string
*/
protected $areaCode;
/**
* Default country code
* @var string
*/
protected $countryCode;
/**
* Default extension
*
* @var string
*/
protected $ext;
/**
* @return NumericField
*/
public function getCountryField()
{
return $this->getChildField('Country');
}
/**
* @return NumericField
*/
public function getAreaField()
{
return $this->getChildField('Area');
}
/**
* @return NumericField
*/
public function getNumberField()
{
return $this->getChildField('Number');
}
/**
* @return NumericField
*/
public function getExtensionField()
{
/** @skipUpgrade */
return $this->getChildField('Extension');
}
protected function getChildField($name)
{
$endsWith = "[{$name}]";
foreach ($this->getChildren() as $child) {
/** @var Formfield $child */
if (substr($child->getName(), 0 - strlen($endsWith)) === $endsWith) {
return $child;
}
}
return null;
}
public function __construct(
$name,
$title = null,
$value = '',
$extension = null,
$areaCode = null,
$countryCode = null
) {
$this->areaCode = $areaCode;
$this->ext = $extension;
$this->countryCode = $countryCode;
// Build fields
$fields = new FieldList();
if ($this->countryCode !== null) {
$countryField = NumericField::create($name.'[Country]', false, $countryCode, 4)
->addExtraClass('phonenumber-field__country');
$fields->push($countryField);
}
if ($this->areaCode !== null) {
$areaField = NumericField::create($name.'[Area]', false, $areaCode, 4)
->addExtraClass('phonenumber-field__area');
$fields->push($areaField);
}
$numberField = NumericField::create($name.'[Number]', false, null, 10)
->addExtraClass('phonenumber-field__number');
$fields->push($numberField);
if ($this->ext !== null) {
$extensionField = NumericField::create($name.'[Extension]', false, $extension, 6)
->addExtraClass('phonenumber-field__extension');
$fields->push($extensionField);
}
parent::__construct($title, $fields);
$this->setName($name);
if (isset($value)) {
$this->setValue($value);
}
}
public function setName($name)
{
parent::setName($name);
foreach ($this->getChildren() as $child) {
/** @var FormField $child */
$thisName = $child->getName();
$thisName = preg_replace('/^.*(\[\\w+\\])$/', $name . '\\1', $thisName);
$child->setName($thisName);
}
}
public function hasData()
{
return true;
}
/**
* @param array $properties
* @return string
*/
public function Field($properties = array())
{
foreach ($this->getChildren() as $field) {
/** @var FormField $field */
$field->setDisabled($this->isDisabled());
$field->setReadonly($this->IsReadonly());
$field->setForm($this->getForm());
}
return parent::Field($properties);
}
public function setValue($value, $data = null)
{
$this->value = self::joinPhoneNumber($value);
$parts = $this->parseValue();
if ($countryField = $this->getCountryField()) {
$countryField->setValue($parts['Country']);
}
if ($areaField = $this->getAreaField()) {
$areaField->setValue($parts['Area']);
}
$this->getNumberField()->setValue($parts['Number']);
if ($extensionField = $this->getExtensionField()) {
/** @skipUpgrade */
$extensionField->setValue($parts['Extension']);
}
return $this;
}
/**
* Join phone number into a string
*
* @param array|string $value Input
* @return string
*/
public static function joinPhoneNumber($value)
{
if (is_array($value)) {
$completeNumber = '';
if (!empty($value['Country'])) {
$completeNumber .= '+' . $value['Country'];
}
if (!empty($value['Area'])) {
$completeNumber .= '(' . $value['Area'] . ')';
}
$completeNumber .= $value['Number'];
/** @skipUpgrade */
if (!empty($value['Extension'])) {
$completeNumber .= '#' . $value['Extension'];
}
return $completeNumber;
} else {
return $value;
}
}
/**
* Returns array with parsed phone format
*
* @return array Array with Country, Area, Number, and Extension keys (in order)
*/
protected function parseValue()
{
if (is_array($this->value)) {
return $this->value;
}
// Parse value in form "+ countrycode (areacode) phone # extension"
$valid = preg_match(
'/^(?:(?:\+(?<Country>\d+))?\s*\((?<Area>\d+)\))?\s*(?<Number>[0-9A-Za-z]*)\s*(?:[#]\s*(?<Extension>\d+))?$/',
$this->value,
$parts
);
if (!$valid) {
$parts = [];
}
/** @skipUpgrade */
return array(
'Country' => isset($parts['Country']) ? $parts['Country'] : '',
'Area' => isset($parts['Area']) ? $parts['Area'] : '',
'Number' => isset($parts['Number']) ? $parts['Number'] : '',
'Extension' => isset($parts['Extension']) ? $parts['Extension'] : '',
);
}
public function saveInto(DataObjectInterface $record)
{
$completeNumber = static::joinPhoneNumber($this->parseValue());
$record->setCastedField($this->getName(), $completeNumber);
}
/**
* Validate this field
*
* @todo Very basic validation at the moment
*
* @param Validator $validator
* @return bool
*/
public function validate($validator)
{
$valid = preg_match(
'/^[0-9\+\-\(\)\s\#]*$/',
$this->joinPhoneNumber($this->value)
);
if (!$valid) {
$validator->validationError(
$this->name,
_t('SilverStripe\\Forms\\PhoneNumberField.VALIDATION', "Please enter a valid phone number"),
"validation"
);
return false;
}
return true;
}
public function performReadonlyTransformation()
{
// Just setReadonly without casting to NumericField_Readonly
$clone = clone $this;
$clone->setReadonly(true);
return $clone;
}
public function performDisabledTransformation()
{
// Just setDisabled without casting to NumericField_Disabled
$clone = clone $this;
$clone->setDisabled(true);
return $clone;
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace SilverStripe\Forms;
/**
* Action that clears all fields on a form.
* Inserts an input tag with type=reset.
*/
class ResetFormAction extends FormAction
{
public function getAttributes()
{
return array_merge(
parent::getAttributes(),
array('type' => 'reset')
);
}
public function Type()
{
return 'resetformaction';
}
}

View File

@ -1,9 +0,0 @@
<span id="{$Name}_Holder" class="creditCardField">
<input $getAttributesHTML('id', 'name', 'value', 'tabindex') name="{$Name}[0]" value="{$ValueOne}" $TabIndexHTML(0)/>
-
<input $getAttributesHTML('id', 'name', 'value', 'tabindex') name="{$Name}[1]" value="{$ValueTwo}" $TabIndexHTML(1)/>
-
<input $getAttributesHTML('id', 'name', 'value', 'tabindex') name="{$Name}[2]" value="{$ValueThree}" $TabIndexHTML(2)/>
-
<input $getAttributesHTML('id', 'name', 'value', 'tabindex') name="{$Name}[3]" value="{$ValueFour}" $TabIndexHTML(3)/>
</span>

View File

@ -1,25 +0,0 @@
<?php
namespace SilverStripe\Forms\Tests;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\InlineFormAction;
/**
* Tests {@see InlineFormAction}
*/
class InlineFormActionTest extends SapphireTest
{
public function testField()
{
$action = new InlineFormAction('dothing', 'My Title', 'ss-action');
$html = (string)$action->Field();
$this->assertContains('<input', $html);
$this->assertContains('type="submit"', $html);
$this->assertContains('name="action_dothing"', $html);
$this->assertContains('value="My Title"', $html);
$this->assertContains('id="dothing"', $html);
$this->assertContains('class="action ss-action"', $html);
}
}