function FormsElementsValidationTestCase::testMaxlengthValidation

Tests #maxlength validation.

File

modules/simpletest/tests/form.test, line 792

Class

FormsElementsValidationTestCase
Tests validation of additional Form API properties.

Code

public function testMaxlengthValidation() {
  $max_length = 5;
  // The field types that support #maxlength.
  $form = array(
    'textfield' => array(
      '#type' => 'textfield',
      '#title' => 'Textfield',
      '#required' => FALSE,
      '#maxlength' => $max_length,
    ),
    'password' => array(
      '#type' => 'password',
      '#title' => 'Password',
      '#maxlength' => $max_length,
    ),
  );
  $edit = array(
    'textfield' => $this->randomString($max_length + 1),
    'password' => $this->randomString($max_length + 1),
  );
  list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, $edit);
  $this->assertFalse(empty($errors), 'Form with overly long inputs returned errors.');
  $this->assertTrue(isset($errors['textfield']) && strpos($errors['textfield'], 'cannot be longer than') !== FALSE, 'Long input error in textfield.');
  $this->assertTrue(isset($errors['password']) && strpos($errors['password'], 'cannot be longer than') !== FALSE, 'Long input error in password.');
  // This test for NULL inputs cannot be performed using the drupalPost() method.
  $edit['textfield'] = NULL;
  $edit['password'] = NULL;
  list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, $edit);
  $this->assertTrue(empty($errors), 'Form with NULL inputs did not return errors.');
  $edit['textfield'] = $this->randomString($max_length);
  $edit['password'] = $this->randomString($max_length);
  list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, $edit);
  $this->assertTrue(empty($errors), 'Form with maxlength inputs did not return errors.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.