sessionless-1.x-dev/tests/modules/sessionless_forms_test/src/Form/SessionlessFormsTestForm.php
tests/modules/sessionless_forms_test/src/Form/SessionlessFormsTestForm.php
<?php
declare(strict_types=1);
namespace Drupal\sessionless_forms_test\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
final class SessionlessFormsTestForm extends FormBase {
public function getFormId() {
return 'sessionless_forms_test_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['text'] = [
'#type' => 'textfield',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
];
// On the first build, there is no user input.
// Form state can not be cached there, as it's a GET request.
if ($form_state->getUserInput()) {
if (!$form_state->get('form_state_test_value')) {
// On the second build, set the value.
$form_state->set('form_state_test_value', 'AliceInWonderland');
}
else {
// On the third build, print what was cached.
$testValue = $form_state->get('form_state_test_value');
$form['test_value'] = [
'#plain_text' => "TestValue:$testValue",
];
}
}
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$form_state->setCached();
$form_state->setRebuild();
}
public function submitForm(array &$form, FormStateInterface $form_state) {}
}
