FormTestFormStateValuesCleanForm.php in Zircon Profile 8.0
File
core/modules/system/tests/modules/form_test/src/Form/FormTestFormStateValuesCleanForm.php
View source
<?php
namespace Drupal\form_test\Form;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class FormTestFormStateValuesCleanForm extends FormBase {
public function getFormId() {
return 'form_test_form_state_clean_values_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = array(
'#tree' => TRUE,
);
$form['foo'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['bar'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['beer'] = array(
'#type' => 'value',
'#value' => 1000,
);
$form['baz']['foo'] = array(
'#type' => 'button',
'#value' => t('Submit'),
);
$form['baz']['baz'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
$form['baz']['beer'] = array(
'#type' => 'value',
'#value' => 2000,
);
$class = get_class($this);
$form['wine'] = [
'#type' => 'value',
'#value' => 3000,
'#process' => [
[
$class,
'cleanValue',
],
],
];
return $form;
}
public static function cleanValue(&$element, FormStateInterface $form_state, &$complete_form) {
$form_state
->addCleanValueKey('wine');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->cleanValues();
print Json::encode($form_state
->getValues());
exit;
}
}