diff options
author | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-01-02 23:30:53 +0000 |
---|---|---|
committer | Angie Byron <webchick@24967.no-reply.drupal.org> | 2010-01-02 23:30:53 +0000 |
commit | 70e53b33c1074655f4ee917c0c4f4b1219bb109d (patch) | |
tree | 7033541b9bc09dfc85aa69be2b57a1f8c8fa9b85 /modules | |
parent | d4f4d3c32e2b7028527b13fc3d63d84576562590 (diff) | |
download | brdo-70e53b33c1074655f4ee917c0c4f4b1219bb109d.tar.gz brdo-70e53b33c1074655f4ee917c0c4f4b1219bb109d.tar.bz2 |
#370537 by chx, sun, effulgentsia, quicksketch, eaton, Heine, and yched: Allow buttons to only validate sections of forms, e.g. More buttons. (with tests)
Diffstat (limited to 'modules')
-rw-r--r-- | modules/field/field.form.inc | 19 | ||||
-rw-r--r-- | modules/poll/poll.module | 20 | ||||
-rw-r--r-- | modules/simpletest/tests/form.test | 21 | ||||
-rw-r--r-- | modules/simpletest/tests/form_test.module | 46 |
4 files changed, 94 insertions, 12 deletions
diff --git a/modules/field/field.form.inc b/modules/field/field.form.inc index 0970c1a6a..a2c03a5c1 100644 --- a/modules/field/field.form.inc +++ b/modules/field/field.form.inc @@ -214,7 +214,7 @@ function field_multiple_value_form($field, $instance, $langcode, $items, &$form, '#name' => $field_name . '_add_more', '#value' => t('Add another item'), '#attributes' => array('class' => array('field-add-more-submit')), - // Submit callback for disabled JavaScript. + '#limit_validation_errors' => array(array($field_name, $langcode)), '#submit' => array('field_add_more_submit'), '#ajax' => array( 'callback' => 'field_add_more_js', @@ -341,9 +341,13 @@ function field_default_form_errors($obj_type, $object, $field, $instance, $langc } /** - * Submit handler to add more choices to a field form. This handler is used when - * JavaScript is not available. It makes changes to the form state and the - * entire form is rebuilt during the page reload. + * Submit handler for the "Add another item" button of a field form. + * + * This handler is run regardless of whether JS is enabled or not. It makes + * changes to the form state. If the button was clicked with JS disabled, then + * the page is reloaded with the complete rebuilt form. If the button was + * clicked with JS enabled, then ajax_form_callback() calls field_add_more_js() + * to return just the changed part of the form. */ function field_add_more_submit($form, &$form_state) { // Set the form to rebuild and run submit handlers. @@ -360,7 +364,12 @@ function field_add_more_submit($form, &$form_state) { } /** - * Ajax callback for addition of new empty widgets. + * Ajax callback in response to a new empty widget being added to the form. + * + * This returns the new page content to replace the page content made obsolete + * by the form submission. + * + * @see field_add_more_submit() */ function field_add_more_js($form, $form_state) { // Retrieve field information. diff --git a/modules/poll/poll.module b/modules/poll/poll.module index 9c48de6b6..42d7afc68 100644 --- a/modules/poll/poll.module +++ b/modules/poll/poll.module @@ -271,7 +271,8 @@ function poll_form($node, &$form_state) { '#value' => t('More choices'), '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."), '#weight' => 1, - '#submit' => array('poll_more_choices_submit'), // If no javascript action. + '#limit_validation_errors' => array(array('choice')), + '#submit' => array('poll_more_choices_submit'), '#ajax' => array( 'callback' => 'poll_choice_js', 'wrapper' => 'poll-choices', @@ -322,9 +323,13 @@ function poll_form($node, &$form_state) { } /** - * Submit handler to add more choices to a poll form. This handler is used when - * javascript is not available. It makes changes to the form state and the - * entire form is rebuilt during the page reload. + * Submit handler to add more choices to a poll form. + * + * This handler is run regardless of whether JS is enabled or not. It makes + * changes to the form state. If the button was clicked with JS disabled, then + * the page is reloaded with the complete rebuilt form. If the button was + * clicked with JS enabled, then ajax_form_callback() calls poll_choice_js() to + * return just the changed part of the form. */ function poll_more_choices_submit($form, &$form_state) { include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'node') . '/node.pages.inc'; @@ -379,7 +384,12 @@ function _poll_choice_form($key, $chid = NULL, $value = '', $votes = 0, $weight } /** - * Menu callback for AHAH additions. Render the new poll choices. + * Ajax callback in response to new choices being added to the form. + * + * This returns the new page content to replace the page content made obsolete + * by the form submission. + * + * @see poll_more_choices_submit() */ function poll_choice_js($form, $form_state) { return $form['choice_wrapper']['choice']; diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index ba6a5d622..108391777 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -231,6 +231,27 @@ class FormValidationTestCase extends DrupalWebTestCase { $this->assertNoFieldByName('name', t('Form element was hidden.')); $this->assertText('Name value: element_validate_access', t('Value for inaccessible form element exists.')); } + + /** + * Tests partial form validation through #limit_validation_errors. + */ + function testValidateLimitErrors() { + $edit = array('test' => 'invalid'); + $path = 'form-test/limit-validation-errors'; + + // Submit the form by pressing the button with #limit_validation_errors and + // ensure that the title field is not validated, but the #element_validate + // handler for the 'test' field is triggered. + $this->drupalPost($path, $edit, t('Partial validate')); + $this->assertNoText(t('!name field is required.', array('!name' => 'Title'))); + $this->assertText('Test element is invalid'); + + // Now test full form validation and ensure that the #element_validate + // handler is still triggered. + $this->drupalPost($path, $edit, t('Full validate')); + $this->assertText(t('!name field is required.', array('!name' => 'Title'))); + $this->assertText('Test element is invalid'); + } } /** diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index 2036204ba..c8f565938 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -17,6 +17,13 @@ function form_test_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['form-test/limit-validation-errors'] = array( + 'title' => 'Form validation with some error suppression', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_limit_validation_errors_form'), + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); $items['form_test/tableselect/multiple-true'] = array( 'title' => 'Tableselect checkboxes test', @@ -204,6 +211,41 @@ function form_test_validate_form_validate(&$form, &$form_state) { } /** + * Builds a simple form with a button triggering partial validation. + */ +function form_test_limit_validation_errors_form($form, &$form_state) { + $form['title'] = array( + '#type' => 'textfield', + '#title' => 'Title', + '#required' => TRUE, + ); + $form['test'] = array( + '#type' => 'textfield', + '#element_validate' => array('form_test_limit_validation_errors_element_validate_test'), + ); + $form['actions']['partial'] = array( + '#type' => 'submit', + '#limit_validation_errors' => array(array('test')), + '#submit' => array(), + '#value' => t('Partial validate'), + ); + $form['actions']['full'] = array( + '#type' => 'submit', + '#value' => t('Full validate'), + ); + return $form; +} + +/** + * Form element validation handler for the 'test' element. + */ +function form_test_limit_validation_errors_element_validate_test(&$element, &$form_state) { + if ($element['#value'] == 'invalid') { + form_error($element, 'Test element is invalid'); + } +} + +/** * Create a header and options array. Helper function for callbacks. */ function _form_test_tableselect_get_data() { @@ -895,7 +937,7 @@ function form_test_state_persist($form, &$form_state) { /** * Submit handler. - * + * * @see form_test_state_persist() */ function form_test_state_persist_submit($form, &$form_state) { @@ -905,7 +947,7 @@ function form_test_state_persist_submit($form, &$form_state) { /** * Implements hook_form_FORM_ID_alter(). - * + * * @see form_test_state_persist() */ function form_test_form_form_test_state_persist_alter(&$form, &$form_state) { |