Skip to content

SS6 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
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
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
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
}
},
"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"
},
"config": {
"allow-plugins": {
Expand Down
46 changes: 20 additions & 26 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 (!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
50 changes: 23 additions & 27 deletions src/Forms/RangeField.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 @@ -26,35 +27,28 @@ public function __construct($name, $title = null, $value = null)
{
parent::__construct($name, $title, $value);
// Set sensible defaults per MDN
$this->setMin(0);
$this->setMax(100);
$this->setStep(1);
$this->setMin('0');
$this->setMax('100');
$this->setStep('1');
}

/**
* Validates for value within range ?
*
* @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;
}

if (!is_numeric($value)) {
$validator->validationError(
$validationResult->addFieldError(
$this->name,
_t('Codem\\Utilities\\HTML5\\RangeField.VALIDATION_NUMERIC', 'Please enter a number value'),
'validation'
ValidationResult::TYPE_ERROR
);
// value is not valid
return false;
return $validationResult;
}

$max = $this->getMax();
Expand All @@ -63,7 +57,7 @@ public function validate($validator)
$valid = $value >= $min && $value <= $max;
if (!$valid) {
// out of range
$validator->validationError(
$validationResult->addFieldError(
$this->name,
_t(
'Codem\\Utilities\\HTML5\\RangeField.VALIDATION_BOUNDS',
Expand All @@ -73,16 +67,16 @@ public function validate($validator)
'max' => $max
]
),
'validation'
ValidationResult::TYPE_ERROR
);
}

return $valid;
return $validationResult;
} elseif (is_numeric($min)) {
$valid = $value >= $min;
if (!$valid) {
// out of range
$validator->validationError(
$validationResult->addFieldError(
$this->name,
_t(
'Codem\\Utilities\\HTML5\\RangeField.VALIDATION_BOUNDS',
Expand All @@ -91,15 +85,16 @@ public function validate($validator)
'min' => $min
]
),
'validation'
ValidationResult::TYPE_ERROR
);
}
return $valid;

return $validationResult;
} elseif (is_numeric($max)) {
$valid = $value <= $max;
if (!$valid) {
// out of range
$validator->validationError(
$validationResult->addFieldError(
$this->name,
_t(
'Codem\\Utilities\\HTML5\\RangeField.VALIDATION_BOUNDS',
Expand All @@ -108,13 +103,14 @@ public function validate($validator)
'max' => $max
]
),
'validation'
ValidationResult::TYPE_ERROR
);
}
return $valid;

return $validationResult;
} else {
// no range restriction, numeric value is valid
return true;
return $validationResult;
}
}

Expand Down
Loading