Skip to content
Draft

SS6 #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.php-cs-fixer.dist.php export-ignore
/phpunit.xml.dist export-ignore
/code-of-conduct.md export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ This works somewhat like a `<select>` element, except that browser implementatio

## Requirements

+ silverstripe/framework ^5 (>= v1)
+ silverstripe/framework ^4 (< v1)
+ silverstripe/framework ^6 (^2)
+ silverstripe/framework ^5 (^1)
+ silverstripe/framework ^4 (<1)

## Installation

Expand Down
20 changes: 13 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,23 @@
}
},
"require": {
"silverstripe/framework" : "^5"
"silverstripe/framework" : "^6"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpunit/phpunit": "^11.5",
"friendsofphp/php-cs-fixer": "^3",
"ext-dom" : "*",
"phpstan/phpstan": "^1",
"rector/rector": "^1",
"nswdpc/ci-files": "dev-v-3",
"cambis/silverstan": "^1",
"cambis/silverstripe-rector": "^1"
"phpstan/phpstan": "^2",
"rector/rector": "^2",
"nswdpc/ci-files": "dev-v-4",
"cambis/silverstan": "^2",
"cambis/silverstripe-rector": "^2"
},
"scripts": {
"phpstan-analyse": "./vendor/bin/phpstan analyse --ansi --no-progress --no-interaction --configuration vendor/nswdpc/ci-files/phpstan/.phpstan.silverstripe.neon",
"rector-dryrun": "./vendor/bin/rector process --dry-run --ansi --config vendor/nswdpc/ci-files/rector/.rector.silverstripe_6_83.php src tests",
"rector-process": "./vendor/bin/rector process --no-diffs --ansi --config vendor/nswdpc/ci-files/rector/.rector.silverstripe_6_83.php src tests",
"phpcsfixer-fix": "./vendor/bin/php-cs-fixer fix --ansi --no-interaction --config vendor/nswdpc/ci-files/php-cs-fixer/.php-cs-fixer.php src tests"
},
"config": {
"allow-plugins": {
Expand Down
25 changes: 13 additions & 12 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="codem/silverstripe-html5-inputs">
<directory>tests/</directory>
</testsuite>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage includeUncoveredFiles="true">
<include>
<directory suffix=".php">src/</directory>
</include>
<exclude>
<directory suffix=".php">tests/</directory>
</exclude>
</coverage>
<testsuite name="codem/silverstripe-html5-inputs">
<directory>tests/</directory>
</testsuite>
</phpunit>
48 changes: 21 additions & 27 deletions src/Forms/ColorField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Codem\Utilities\HTML5;

use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Forms\FormField;
use SilverStripe\Forms\Validator;

Expand Down Expand Up @@ -68,11 +69,8 @@ public function dataValue()
return $this->getValidRGB($this->value);
}

/**
* @return string
*/
#[\Override]
public function Value()
public function getValue(): mixed
{
return $this->dataValue();
}
Expand Down Expand Up @@ -112,7 +110,7 @@ public function getDefaultValue(): string
/**
* Base on the value return either the defaultValue colour value or the value
* @param string $value the value to check
* @param Validator $validator optional, see isValidRGB
* @param ValidationResult|null $validationResult optional, see isValidRGB
*
* The only valid value here is defined by the "valid simple colour" definition at
* https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#valid-simple-colour
Expand All @@ -123,7 +121,7 @@ public function getDefaultValue(): string
* representing the red component, the middle two digits representing the green component,
* and the last two digits representing the blue component, in hexadecimal.</blockquote>
*/
public function getValidRGB(?string $value, Validator $validator = null): string
public function getValidRGB(?string $value, ValidationResult &$validationResult = null): string
{

$simpleColourValue = $this->defaultValue ?? static::WHITE;
Expand All @@ -134,7 +132,7 @@ public function getValidRGB(?string $value, Validator $validator = null): string
}

$value = strtolower($value);
if (!$this->isValidRGB($value, $validator)) {
if (!$this->isValidRGB($value, $validationResult)) {
$value = $simpleColourValue;
}

Expand All @@ -145,24 +143,24 @@ public function getValidRGB(?string $value, Validator $validator = null): string
/**
* Check if the value provided is a valid RGB value
* @param string $value
* @param Validator $validator an optional validator. If provided specific errors will be stored in the validator
* @param ValidationResult|null $validationResult an optional validator. If provided specific errors will be stored in the validator
*/
public function isValidRGB(?string $value, Validator $validator = null): bool
public function isValidRGB(?string $value, ValidationResult &$validationResult = null): bool
{

// Ensure a string value
$value ??= '';

// If input is not exactly seven characters long, then return an error.
if (mb_strlen($value) != 7) {
if ($validator instanceof \SilverStripe\Forms\Validator) {
$validator->validationError(
if (mb_strlen($value) !== 7) {
if (!is_null($validationResult)) {
$validationResult->addFieldError(
$this->name,
_t(
'Codem\\Utilities\\HTML5\\ColorField.VALIDATE_NOT_VALID_RGB',
'The colour value must be a 6 character RGB colour value in the range #000000 to #ffffff'
),
"validation"
ValidationResult::TYPE_ERROR
);
}

Expand All @@ -171,14 +169,14 @@ public function isValidRGB(?string $value, Validator $validator = null): bool

// If the first character in input is not a U+0023 NUMBER SIGN character (#), then return an error.
if (!str_starts_with($value, "#")) {
if ($validator instanceof \SilverStripe\Forms\Validator) {
$validator->validationError(
if (!is_null($validationResult)) {
$validationResult->addFieldError(
$this->name,
_t(
'Codem\\Utilities\\HTML5\\ColorField.VALIDATE_MISSING_HASH',
'The colour value must start with a # character'
),
"validation"
ValidationResult::TYPE_ERROR
);
}

Expand All @@ -188,14 +186,14 @@ public function isValidRGB(?string $value, Validator $validator = null): bool
// If the last six characters of input are not all ASCII hex digits, then return an error.
$hex = trim($value, "#");
if (ctype_xdigit($hex) === false) {
if ($validator instanceof \SilverStripe\Forms\Validator) {
$validator->validationError(
if (!is_null($validationResult)) {
$validationResult->addFieldError(
$this->name,
_t(
'Codem\\Utilities\\HTML5\\ColorField.VALIDATE_NOT_VALID_RGB',
'The colour value must be a valid 6 character RGB colour value in the range #000000 to #ffffff'
),
"validation"
ValidationResult::TYPE_ERROR
);
}

Expand All @@ -206,16 +204,12 @@ public function isValidRGB(?string $value, Validator $validator = null): bool
return true;
}

/**
* Validate this field
*
* @param Validator $validator
* @return bool
*/
#[\Override]
public function validate($validator)
public function validate(): ValidationResult
{
return $this->isValidRGB($this->Value(), $validator);
$validationResult = parent::validate();
$this->isValidRGB($this->getValue(), $validationResult);
return $validationResult;
}

}
23 changes: 9 additions & 14 deletions src/Forms/DateField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Codem\Utilities\HTML5;

use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Forms\TextField;

/**
Expand Down Expand Up @@ -54,21 +55,15 @@ public function setMax(\DateTime $max): static
return $this->setAttribute('max', $this->formatDate($max));
}

/**
* Validates for date value in format specified
*
* @param \SilverStripe\Forms\Validator $validator
*
* @return bool
*/
#[\Override]
public function validate($validator)
public function validate(): ValidationResult
{
try {
$value = trim($this->Value() ?? '');
$validationResult = parent::validate();
$value = trim($this->getValue() ?? '');
if ($value === '') {
// empty values are valid
return true;
return $validationResult;
}

$dt = new \Datetime($value);
Expand All @@ -77,9 +72,9 @@ public function validate($validator)
throw new \Exception("Invalid date value passed");
}

return true;
return $validationResult;
} catch (\Exception) {
$validator->validationError(
$validationResult->addFieldError(
$this->name,
_t(
'Codem\\Utilities\\HTML5\\DateField.VALIDATION',
Expand All @@ -89,9 +84,9 @@ public function validate($validator)
'example' => $this->example
]
),
'validation'
ValidationResult::TYPE_ERROR
);
return false;
return $validationResult;
}
}

Expand Down
23 changes: 9 additions & 14 deletions src/Forms/NumberField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Codem\Utilities\HTML5;

use SilverStripe\Core\Validation\ValidationResult;
use SilverStripe\Forms\TextField;

/**
Expand All @@ -17,29 +18,23 @@ class NumberField extends TextField

protected $inputType = 'number';

/**
* Validates for numeric value
*
* @param \SilverStripe\Forms\Validator $validator
*
* @return bool
*/
#[\Override]
public function validate($validator)
public function validate(): ValidationResult
{
$value = trim($this->Value() ?? '');
$validationResult = parent::validate();
$value = trim($this->getValue() ?? '');
if ($value === '') {
// empty values are valid
return true;
return $validationResult;
} elseif (!is_numeric($value)) {
$validator->validationError(
$validationResult->addFieldError(
$this->name,
_t('Codem\\Utilities\\HTML5\\NumberField.VALIDATION', 'Please enter a number value'),
'validation'
ValidationResult::TYPE_ERROR
);
return false;
return $validationResult;
} else {
return true;
return $validationResult;
}
}

Expand Down
Loading