3 namespace Drupal\form_test\Form;
5 use Drupal\Core\Form\FormBase;
6 use Drupal\Core\Form\FormStateInterface;
7 use Symfony\Component\HttpFoundation\JsonResponse;
9 class FormTestInputForgeryForm extends FormBase {
14 public function getFormId() {
15 return '_form_test_input_forgery';
21 public function buildForm(array $form, FormStateInterface $form_state) {
22 // For testing that a user can't submit a value not matching one of the
24 $form['checkboxes'] = [
25 '#title' => t('Checkboxes'),
26 '#type' => 'checkboxes',
34 '#value' => t('Submit'),
36 $form['#post_render'][] = [static::class, 'postRender'];
42 * Alters the rendered form to simulate input forgery.
44 * It's necessary to alter the rendered form here because Mink does not
45 * support manipulating the DOM tree.
47 * @param string $rendered_form
51 * The modified rendered form.
53 * @see \Drupal\Tests\system\Functional\Form\FormTest::testInputForgery()
55 public static function postRender($rendered_form) {
56 return str_replace('value="two"', 'value="FORGERY"', $rendered_form);
62 public function submitForm(array &$form, FormStateInterface $form_state) {
63 return new JsonResponse($form_state->getValues());