0

I created a form that I wanted to be reusable. So I created to form below to imported into a add form and a edit form. Please be aware I do have the validateForm, and submitForm function in my code.

class PaneConfigurationForm extends ConfigFormBase {

    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'admin_pane_settings';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state, $element = null) {

       $form['description'] = array(
          '#type' => 'text_format',
          '#title' => 'description',
          '#default_value' => '<p>The quick brown fox jumped over the lazy dog.</p>',
          '#value' => '<p>The quick brown fox jumped over the lazy dog.</p>',
          '#format' => 'full_html',
       );

       return parent::buildForm($form, $form_state);
    }
}

So in my AddPane form I use the \Drupal::formBuilder()->getForm() function to get the form and apply it to my parent form. For some reason when I submit the form the value is not passed to the submit handler, only to form format.

class AddPaneForm extends Formbase {

    /**
     * {@inheritdoc}
     */
    public function getFormId() {
        return 'add_admin_pane';
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state, $element = null) {


          $form = \Drupal::formBuilder()->getForm('Drupal\_module_\Form\PaneConfigurationForm');

          $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => $this->t('Save'),
          );
    }

    public function submitForm(array &$form, FormStateInterface $form_state)
    {
          $form_state->getValue('description')
    }
}

What can I do to allow the format and value to be passed to the submit handler, not just the text_format field's format.

0

Just remove the #value:

$form['description'] = array(
  '#type' => 'text_format',
  '#title' => 'description',
  '#default_value' => '<p>The quick brown fox jumped over the lazy dog.</p>',
  '#format' => 'full_html',
);

#value permeates all the way through a form build/validation/submission, and doesn't get replaced with user input.

#default_value will cover the initial value when the form loads.

  • If I remove value, I get an error on the AddPaneForm page that says it is needed. Also when I save the AddPaneForm page $form_state->getValue('description') still doesn't get the value. Only the format. – J.Floyd Jul 24 '18 at 15:31
  • 1
    I'm not sure what's causing that - you should only use #value if you want to override user input. It's not a required property, that must be something configured specifically on your site somehow. Search the /core folder for '#type' => 'text_format' and you'll find dozens of examples of the same thing in core that don't have the #value property (and work fine) – Clive Jul 24 '18 at 15:59
  • I know. I think the problem may stem from the fact that I'm an using the \Drupal::formBuilder()->getForm() get form function to pull in another form. I'm sure is I used a pure '#type' => 'text_format' instead of one being rendered via the \Drupal::formBuilder()->getForm() everything would work as intended. I just can't figure out why \Drupal::formBuilder()->getForm() causes such an issue. – J.Floyd Jul 25 '18 at 17:46
  • were you able to see that? – J.Floyd Jul 26 '18 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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