1

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,


enter image description here


The parameter is passed in the HTTP request,


enter image description here


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.

0

1 Answer 1

1

You should remove the #value property. This is what is blanking it out. If you want it blank to start with, set #default_value instead.

See: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21Textarea.php/class/Textarea/8.6.x

1
  • I think that did it! Thanks! I'm not entirely sure because I was trying some other things on my own, as well as your edit (which I caught before you removed it again :) But the #value change was the only substantial thing I changed in the last 30 minutes so Im confident that did it.
    – Jeff
    Commented Dec 12, 2018 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.