Issue #3397491 by borisson_, phenaproxima, Wim Leers, smustgrave: Add validation constraints to core.date_format.

merge-requests/5425/merge
catch 2024-02-12 12:36:41 +00:00
parent 47e76cea08
commit 9df6250df8
3 changed files with 31 additions and 1 deletions

View File

@ -561,7 +561,7 @@ core.date_format.*:
label: 'Date format'
mapping:
id:
type: string
type: machine_name
label: 'ID'
label:
type: required_label
@ -572,6 +572,15 @@ core.date_format.*:
pattern:
type: core_date_format_pattern.[%parent.locked]
label: 'PHP date format'
constraints:
NotBlank: []
# A valid date format character must appear somewhere in the value.
# See https://www.php.net/manual/en/datetime.format.php
Regex:
pattern: '/[aABcdDeFgGhHiIjlLmMnNoOpPrsStTuUvwWxXyYzZ]/'
message: 'This is not a valid date format.'
constraints:
FullyValidatable: ~
# Unlocked date formats should use the translatable type.
core_date_format_pattern.0:

View File

@ -69,6 +69,7 @@ class DateFormatAccessControlHandlerTest extends KernelTestBase {
? ['locked' => FALSE]
: ['locked' => TRUE];
$entity_values['id'] = $entity_values['label'] = $this->randomMachineName();
$entity_values['pattern'] = 'Y-m-d';
$entity = DateFormat::create($entity_values);
$entity->save();

View File

@ -27,4 +27,24 @@ class DateFormatValidationTest extends ConfigEntityValidationTestBase {
$this->entity->save();
}
/**
* Tests that the pattern of a date format is validated.
*
* @param string $pattern
* The pattern to set.
* @param bool $locked
* Whether the date format entity is locked or not.
* @param string $expected_error
* The error message that should be flagged for the invalid pattern.
*
* @testWith ["q", true, "This is not a valid date format."]
* ["", true, "This value should not be blank."]
* ["q", false, "This is not a valid date format."]
* ["", false, "This value should not be blank."]
*/
public function testPatternIsValidated(string $pattern, bool $locked, string $expected_error): void {
$this->entity->setPattern($pattern)->set('locked', $locked);
$this->assertValidationErrors(['pattern' => $expected_error]);
}
}