Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
drupal
Commits
4e93a419
Commit
4e93a419
authored
7 months ago
by
catch
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3485452
by riyas_nr, mjmorley, smustgrave: Datetime fields should utilise #required_error
parent
aea38fc2
Branches
Branches containing commit
Tags
Tags containing commit
4 merge requests
!3478
Issue #3337882: Deleted menus are not removed from content type config
,
!579
Issue #2230909: Simple decimals fail to pass validation
,
!459
Resolve #3118590 "More tests"
,
!213
Issue #2906496: Give Media a menu item under Content
Pipeline
#472610
passed with warnings
7 months ago
Stage: 🪄 Lint
Stage: 🗜️ Test
Pipeline: drupal
#472624
Pipeline: drupal
#472620
Pipeline: drupal
#472615
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/lib/Drupal/Core/Datetime/Element/Datetime.php
+6
-1
6 additions, 1 deletion
core/lib/Drupal/Core/Datetime/Element/Datetime.php
core/tests/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php
+48
-0
48 additions, 0 deletions
...pal/KernelTests/Core/Datetime/DatetimeElementFormTest.php
with
54 additions
and
1 deletion
core/lib/Drupal/Core/Datetime/Element/Datetime.php
+
6
−
1
View file @
4e93a419
...
...
@@ -357,8 +357,13 @@ public static function validateDatetime(&$element, FormStateInterface $form_stat
// If there's empty input and the field is required, set an error. A
// reminder of the required format in the message provides a good UX.
elseif
(
empty
(
$input
[
'date'
])
&&
empty
(
$input
[
'time'
])
&&
$element
[
'#required'
])
{
if
(
isset
(
$element
[
'#required_error'
]))
{
$form_state
->
setError
(
$element
,
$element
[
'#required_error'
]);
}
else
{
$form_state
->
setError
(
$element
,
t
(
'The %field date is required.'
,
[
'%field'
=>
$title
]));
}
}
else
{
// If the date is valid, set it.
$date
=
$input
[
'object'
];
...
...
...
...
This diff is collapsed.
Click to expand it.
core/tests/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php
+
48
−
0
View file @
4e93a419
...
...
@@ -99,6 +99,29 @@ public function buildForm(array $form, FormStateInterface $form_state, string $d
'#date_time_element'
=>
'HTML Time'
,
];
// Element with #required_error.
$form
[
'datetime_required_error'
]
=
[
'#type'
=>
'datetime'
,
'#title'
=>
'Datetime with required error'
,
'#date_date_format'
=>
'Y-m-d'
,
'#date_time_format'
=>
'H:i:s'
,
'#date_date_element'
=>
'HTML Date'
,
'#date_time_element'
=>
'HTML Time'
,
'#required'
=>
TRUE
,
'#required_error'
=>
'Custom required error message.'
,
];
// Element without #required_error.
$form
[
'datetime_no_required_error'
]
=
[
'#type'
=>
'datetime'
,
'#title'
=>
'Datetime without required error'
,
'#date_date_format'
=>
'Y-m-d'
,
'#date_time_format'
=>
'H:i:s'
,
'#date_date_element'
=>
'HTML Date'
,
'#date_time_element'
=>
'HTML Time'
,
'#required'
=>
TRUE
,
];
$form
[
'submit'
]
=
[
'#type'
=>
'submit'
,
'#value'
=>
'Submit'
,
...
...
@@ -210,4 +233,29 @@ public static function trustedCallbacks() {
];
}
/**
* Tests the custom required error message for datetime elements.
*/
public
function
testDatetimeElementRequiredError
():
void
{
$form_builder
=
$this
->
container
->
get
(
'form_builder'
);
// Test datetime element with #required_error.
$form_state
=
(
new
FormState
())
->
setValues
([
'datetime_required_error'
=>
''
,
]);
$form_builder
->
submitForm
(
$this
,
$form_state
);
// Check that the custom required error message is set correctly.
$this
->
assertEquals
(
'Custom required error message.'
,
$form_state
->
getErrors
()[
'datetime_required_error'
]);
// Test datetime element without #required_error.
$form_state
=
(
new
FormState
())
->
setValues
([
'datetime_no_required_error'
=>
''
,
]);
$form_builder
->
submitForm
(
$this
,
$form_state
);
// Check that the default required error message is set correctly.
$this
->
assertEquals
(
'The Datetime without required error date is required.'
,
$form_state
->
getErrors
()[
'datetime_no_required_error'
]);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
sign in
to comment