summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--includes/form.inc7
-rw-r--r--modules/simpletest/tests/form.test9
-rw-r--r--modules/simpletest/tests/form_test.module34
3 files changed, 49 insertions, 1 deletions
diff --git a/includes/form.inc b/includes/form.inc
index b639fa6..9018931 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -661,9 +661,14 @@ function drupal_form_submit($form_id, &$form_state) {
// Merge in default values.
$form_state += form_state_defaults();
- $form = drupal_retrieve_form($form_id, $form_state);
+ // Populate $form_state['input'] with the submitted values before retrieving
+ // the form, to be consistent with what drupal_build_form() does for
+ // non-programmatic submissions (form builder functions may expect it to be
+ // there).
$form_state['input'] = $form_state['values'];
+
$form_state['programmed'] = TRUE;
+ $form = drupal_retrieve_form($form_id, $form_state);
// Programmed forms are always submitted.
$form_state['submitted'] = TRUE;
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test
index 3debf40..b2b8223 100644
--- a/modules/simpletest/tests/form.test
+++ b/modules/simpletest/tests/form.test
@@ -1222,6 +1222,15 @@ class FormsProgrammaticTestCase extends DrupalWebTestCase {
$this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => NULL, 2 => 2)), TRUE);
$this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => NULL, 2 => NULL)), TRUE);
+ // Test that a programmatic form submission can correctly click a button
+ // that limits validation errors based on user input. Since we do not
+ // submit any values for "textfield" here and the textfield is required, we
+ // only expect form validation to pass when validation is limited to a
+ // different field.
+ $this->submitForm(array('op' => 'Submit with limited validation', 'field_to_validate' => 'all'), FALSE);
+ $this->submitForm(array('op' => 'Submit with limited validation', 'field_to_validate' => 'textfield'), FALSE);
+ $this->submitForm(array('op' => 'Submit with limited validation', 'field_to_validate' => 'field_to_validate'), TRUE);
+
// Restore the current batch status.
$batch = $current_batch;
}
diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module
index a816caa..fadd2aa 100644
--- a/modules/simpletest/tests/form_test.module
+++ b/modules/simpletest/tests/form_test.module
@@ -1377,6 +1377,7 @@ function form_test_programmatic_form($form, &$form_state) {
'#title' => 'Textfield',
'#type' => 'textfield',
);
+
$form['checkboxes'] = array(
'#type' => 'checkboxes',
'#options' => array(
@@ -1388,6 +1389,39 @@ function form_test_programmatic_form($form, &$form_state) {
'#default_value' => array(1, 2),
);
+ $form['field_to_validate'] = array(
+ '#type' => 'radios',
+ '#title' => 'Field to validate (in the case of limited validation)',
+ '#description' => 'If the form is submitted by clicking the "Submit with limited validation" button, then validation can be limited based on the value of this radio button.',
+ '#options' => array(
+ 'all' => 'Validate all fields',
+ 'textfield' => 'Validate the "Textfield" field',
+ 'field_to_validate' => 'Validate the "Field to validate" field',
+ ),
+ '#default_value' => 'all',
+ );
+
+ // The main submit button for the form.
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => 'Submit',
+ );
+ // A secondary submit button that allows validation to be limited based on
+ // the value of the above radio selector.
+ $form['submit_limit_validation'] = array(
+ '#type' => 'submit',
+ '#value' => 'Submit with limited validation',
+ // Use the same submit handler for this button as for the form itself.
+ // (This must be set explicitly or otherwise the form API will ignore the
+ // #limit_validation_errors property.)
+ '#submit' => array('form_test_programmatic_form_submit'),
+ );
+ if (!empty($form_state['input']['field_to_validate']) && $form_state['input']['field_to_validate'] != 'all') {
+ $form['submit_limit_validation']['#limit_validation_errors'] = array(
+ array($form_state['input']['field_to_validate']),
+ );
+ }
+
return $form;
}