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
0dca13c6
Commit
0dca13c6
authored
Mar 12, 2014
by
webchick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue
#2214055
by sun: Programmed form submission does not get a triggering_element.
parent
3ed3cbe0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
106 additions
and
0 deletions
+106
-0
core/lib/Drupal/Core/Form/FormBuilder.php
core/lib/Drupal/Core/Form/FormBuilder.php
+9
-0
core/modules/system/lib/Drupal/system/Tests/Form/TriggeringElementProgrammedUnitTest.php
...system/Tests/Form/TriggeringElementProgrammedUnitTest.php
+97
-0
No files found.
core/lib/Drupal/Core/Form/FormBuilder.php
View file @
0dca13c6
...
...
@@ -553,6 +553,15 @@ public function processForm($form_id, &$form, &$form_state) {
if
(
isset
(
$form
[
'#token'
])
&&
$form
[
'#token'
]
===
FALSE
)
{
unset
(
$form
[
'#token'
]);
}
// Form values for programmed form submissions typically do not include a
// value for the submit button. But without a triggering element, a
// potentially existing #limit_validation_errors property on the primary
// submit button is not taken account. Therefore, check whether there is
// exactly one submit button in the form, and if so, automatically use it
// as triggering_element.
if
(
$form_state
[
'programmed'
]
&&
!
isset
(
$form_state
[
'triggering_element'
])
&&
count
(
$form_state
[
'buttons'
])
==
1
)
{
$form_state
[
'triggering_element'
]
=
reset
(
$form_state
[
'buttons'
]);
}
$this
->
validateForm
(
$form_id
,
$form
,
$form_state
);
// drupal_html_id() maintains a cache of element IDs it has seen, so it
...
...
core/modules/system/lib/Drupal/system/Tests/Form/TriggeringElementProgrammedUnitTest.php
0 → 100644
View file @
0dca13c6
<?php
/**
* @file
* Contains \Drupal\system\Tests\Form\TriggeringElementProgrammedUnitTest.
*/
namespace
Drupal\system\Tests\Form
;
use
Drupal\Component\Utility\String
;
use
Drupal\Core\Form\FormInterface
;
use
Drupal\simpletest\DrupalUnitTestBase
;
/**
* Tests detection of triggering_element for programmed form submissions.
*/
class
TriggeringElementProgrammedUnitTest
extends
DrupalUnitTestBase
implements
FormInterface
{
public
static
$modules
=
array
(
'system'
);
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'Form triggering element programmed determination'
,
'description'
=>
'Tests detection of triggering_element for programmed form submissions.'
,
'group'
=>
'Form API'
,
);
}
/**
* {@inheritdoc}
*/
public
function
getFormId
()
{
return
'triggering_element_programmed_form'
;
}
/**
* {@inheritdoc}
*/
public
function
buildForm
(
array
$form
,
array
&
$form_state
)
{
$form
[
'one'
]
=
array
(
'#type'
=>
'textfield'
,
'#title'
=>
'One'
,
'#required'
=>
TRUE
,
);
$form
[
'two'
]
=
array
(
'#type'
=>
'textfield'
,
'#title'
=>
'Two'
,
'#required'
=>
TRUE
,
);
$form
[
'actions'
]
=
array
(
'#type'
=>
'actions'
);
$form
[
'actions'
][
'submit'
]
=
array
(
'#type'
=>
'submit'
,
'#value'
=>
'Save'
,
'#limit_validation_errors'
=>
array
(
array
(
$form_state
[
'input'
][
'section'
]),
),
// Required for #limit_validation_errors.
'#submit'
=>
array
(
array
(
$this
,
'submitForm'
)),
);
return
$form
;
}
/**
* {@inheritdoc}
*/
public
function
validateForm
(
array
&
$form
,
array
&
$form_state
)
{
// Verify that the only submit button was recognized as triggering_element.
$this
->
assertEqual
(
$form
[
'actions'
][
'submit'
][
'#array_parents'
],
$form_state
[
'triggering_element'
][
'#array_parents'
]);
}
/**
* {@inheritdoc}
*/
public
function
submitForm
(
array
&
$form
,
array
&
$form_state
)
{
}
/**
* Tests that #limit_validation_errors of the only submit button takes effect.
*/
function
testLimitValidationErrors
()
{
// Programmatically submit the form.
$form_state
[
'values'
]
=
array
();
$form_state
[
'values'
][
'section'
]
=
'one'
;
$form_builder
=
$this
->
container
->
get
(
'form_builder'
);
$form_builder
->
submitForm
(
$this
,
$form_state
);
// Verify that only the specified section was validated.
$errors
=
$form_builder
->
getErrors
(
$form_state
);
$this
->
assertTrue
(
isset
(
$errors
[
'one'
]),
"Section 'one' was validated."
);
$this
->
assertFalse
(
isset
(
$errors
[
'two'
]),
"Section 'two' was not validated."
);
// Verify that there are only values for the specified section.
$this
->
assertTrue
(
isset
(
$form_state
[
'values'
][
'one'
]),
"Values for section 'one' found."
);
$this
->
assertFalse
(
isset
(
$form_state
[
'values'
][
'two'
]),
"Values for section 'two' not found."
);
}
}
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