I've got a form field that is empty (""
) when I submit my form and I cant figure out where I am going wrong. The field in question is reason
- as in the reason why this record is being changed. I also include vid
because that field is set and passed properly. Full source code of the relevant form here
Form:
public function buildForm(array $form, FormStateInterface $form_state, $part = NULL, $vid = NULL) {
$form = array();
//...
$form['wrapper_form_fields']['reason'] = [
'#type' => 'textarea',
'#title' => '<span class="required">*</span>' . t('Reason:'),
'#value' => '',
'#rows' => 2,
'#attributes' => [
'maxlength' => 250,
'class' => ['js-modal-reason'],
],
'#prefix' => '<div class="reason-wrapper">',
'#suffix' => '</div>',
];
$form['vid'] = array(
'#type' => 'hidden',
'#value' => $vid,
);
//...
return $form;
}
It appears to render out properly,
The parameter is passed in the HTTP request,
But when I dump it out in submitForm
I get a blank value.
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$reason = $form_state->getValue('reason');
echo "<pre>";
var_dump($reason);
var_dump($form_state->getValues());
echo "</pre>";
die;
//...
}
Output:
<pre>string(0) ""
array(7) {
["vid"]=>
string(5) "78897"
["reason"]=>
string(0) ""
["send"]=>
object(Drupal\Core\StringTranslation\TranslatableMarkup)#1021 (5) {
...
Full source code of the relevant form here. One notable design decision is that most of the logic is in a parent class. I have a parent class InfusionCenterStatusForm
that creates forms that will change the status of an instance of a content type. This base class is refined in child classes like InfusionCenterInactiveForm
which will specialize the base form to set a record to be inactive
.