Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
drupal
Commits
79f17dcb
Commit
79f17dcb
authored
May 03, 2016
by
alexpott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2696353
by mpdonadio, dpovshed: Bad dates in Select List widget throw an exception
parent
ef39eed7
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
77 additions
and
5 deletions
+77
-5
core/lib/Drupal/Core/Datetime/Element/Datelist.php
core/lib/Drupal/Core/Datetime/Element/Datelist.php
+8
-3
core/modules/system/src/Tests/System/DateTimeTest.php
core/modules/system/src/Tests/System/DateTimeTest.php
+69
-2
No files found.
core/lib/Drupal/Core/Datetime/Element/Datelist.php
View file @
79f17dcb
...
...
@@ -61,7 +61,12 @@ public static function valueCallback(&$element, $input, FormStateInterface $form
unset
(
$input
[
'ampm'
]);
}
$timezone
=
!
empty
(
$element
[
'#date_timezone'
])
?
$element
[
'#date_timezone'
]
:
NULL
;
$date
=
DrupalDateTime
::
createFromArray
(
$input
,
$timezone
);
try
{
$date
=
DrupalDateTime
::
createFromArray
(
$input
,
$timezone
);
}
catch
(
\
Exception
$e
)
{
$form_state
->
setError
(
$element
,
t
(
'Selected combination of day and month is not valid.'
));
}
if
(
$date
instanceof
DrupalDateTime
&&
!
$date
->
hasErrors
())
{
static
::
incrementRound
(
$date
,
$increment
);
}
...
...
@@ -318,8 +323,8 @@ public static function validateDatelist(&$element, FormStateInterface $form_stat
if
(
$date
instanceof
DrupalDateTime
&&
!
$date
->
hasErrors
())
{
$form_state
->
setValueForElement
(
$element
,
$date
);
}
// If the input is invalid
, set
an error.
else
{
// If the input is invalid
and
an error
doesn't exist, set one
.
else
if
(
$form_state
->
getError
(
$element
)
===
NULL
)
{
$form_state
->
setError
(
$element
,
t
(
'The %field date is invalid.'
,
array
(
'%field'
=>
!
empty
(
$element
[
'#title'
])
?
$element
[
'#title'
]
:
''
)));
}
}
...
...
core/modules/system/src/Tests/System/DateTimeTest.php
View file @
79f17dcb
...
...
@@ -19,13 +19,20 @@ class DateTimeTest extends WebTestBase {
*
* @var array
*/
public
static
$modules
=
[
'block'
,
'node'
,
'language'
];
public
static
$modules
=
[
'block'
,
'node'
,
'language'
,
'field'
,
'field_ui'
,
'datetime'
,
'options'
];
protected
function
setUp
()
{
parent
::
setUp
();
// Create admin user and log in admin user.
$this
->
drupalLogin
(
$this
->
drupalCreateUser
(
array
(
'administer site configuration'
)));
$this
->
drupalLogin
(
$this
->
drupalCreateUser
(
array
(
'administer site configuration'
,
'administer content types'
,
'administer nodes'
,
'administer node fields'
,
'administer node form display'
,
'administer node display'
,
)));
$this
->
drupalPlaceBlock
(
'local_actions_block'
);
}
...
...
@@ -157,4 +164,64 @@ function testDateFormatConfiguration() {
$this
->
assertEscaped
(
'<em>'
.
date
(
"Y"
)
.
'</em>'
);
}
/**
* Test handling case with invalid data in selectors (like February, 31st).
*/
function
testEnteringDateTimeViaSelectors
()
{
$this
->
drupalCreateContentType
(
array
(
'type'
=>
'page_with_date'
,
'name'
=>
'Page with date'
));
$this
->
drupalGet
(
'admin/structure/types/manage/page_with_date'
);
$this
->
assertResponse
(
200
,
'Content type created.'
);
$this
->
drupalGet
(
'admin/structure/types/manage/page_with_date/fields/add-field'
);
$edit
=
array
(
'new_storage_type'
=>
'datetime'
,
'label'
=>
'dt'
,
'field_name'
=>
'dt'
,
);
$this
->
drupalPostForm
(
'admin/structure/types/manage/page_with_date/fields/add-field'
,
$edit
,
t
(
'Save and continue'
));
$this
->
assertText
(
t
(
'These settings apply to the'
),
'New datetime field created, now configuring'
);
$this
->
drupalGet
(
'admin/structure/types/manage/page_with_date/fields/node.page_with_date.field_dt/storage'
);
$edit
=
array
(
'settings[datetime_type]'
=>
'datetime'
,
'cardinality'
=>
'number'
,
'cardinality_number'
=>
'1'
,
);
$this
->
drupalPostForm
(
'admin/structure/types/manage/page_with_date/fields/node.page_with_date.field_dt/storage'
,
$edit
,
t
(
'Save field settings'
));
$this
->
drupalGet
(
'admin/structure/types/manage/page_with_date/fields'
);
$this
->
assertText
(
'field_dt'
,
'New field is in place'
);
$this
->
drupalGet
(
'admin/structure/types/manage/page_with_date/form-display'
);
$edit
=
array
(
'fields[field_dt][type]'
=>
'datetime_datelist'
,
);
$this
->
drupalPostForm
(
'admin/structure/types/manage/page_with_date/form-display'
,
$edit
,
t
(
'Save'
));
$this
->
drupalLogout
();
// Now log in as a regular editor.
$this
->
drupalLogin
(
$this
->
drupalCreateUser
(
array
(
'create page_with_date content'
)));
$this
->
drupalGet
(
'node/add/page_with_date'
);
$edit
=
array
(
'title[0][value]'
=>
'sample doc'
,
'field_dt[0][value][year]'
=>
'2016'
,
'field_dt[0][value][month]'
=>
'2'
,
'field_dt[0][value][day]'
=>
'31'
,
'field_dt[0][value][hour]'
=>
'1'
,
'field_dt[0][value][minute]'
=>
'30'
,
);
$this
->
drupalPostForm
(
'node/add/page_with_date'
,
$edit
,
t
(
'Save'
));
$this
->
assertText
(
t
(
'Selected combination of day and month is not valid.'
),
'Inorrect date failed validation'
);
$edit
[
'field_dt[0][value][day]'
]
=
'29'
;
$this
->
drupalPostForm
(
'node/add/page_with_date'
,
$edit
,
t
(
'Save'
));
$this
->
assertNoText
(
t
(
'Selected combination of day and month is not valid.'
),
'Correct date passed validation.'
);
$this
->
drupalGet
(
'node/1'
);
$this
->
assertText
(
t
(
'Mon, 02/29/2016 - 01:30'
),
'Node successfully created with valid date.'
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment