0

I have a form with a value field and i would like to set the value to a markup field as shown below. The problem is that it retuns empty. Is there other alternatives.

public function buildForm(array $form, FormStateInterface $form_state) {
     $form['container']['#tree'] = TRUE;
     $form['container']['#type'] = 'container';
     $form['container']['store_value'] = [
         '#type' => 'value',
         '#value' => 'Angeline Mnene'
     ];
     $form['container']['show_value'] = [
         '#type' => 'markup',
         '#markup' => 'The stored value is: ' . $form_state-
          >getValue(['container','store_value']),
     ];

     return $form;
}

1 Answer 1

0

I finally got an answer and would like to post it here, to help someone who may run into this problem as well.

public function buildForm(array $form, FormStateInterface $form_state) {

     //SET THE VALUE HERE
     $form_state->setValue('store_value', 'The stored value is: Angeline 
     Mnene');

     $form['container']['#tree'] = TRUE;
     $form['container']['#type'] = 'container';
     $form['container']['show_value'] = [
         '#type' => 'markup',
         '#markup' => 'The stored value is: ' . $form_state-
          >getValue('store_value'),
     ];

     return $form;
}

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.