-1

So i have a simple form that extends the FormBase and when i call the form i pass it an argument, depending on what the user selects within the form itself i rebuild the form and i need to pass a new argument to the buildForm, how do i do this?

class MyForm extends FormBase {
    public function buildForm(array $form, FormStateInterface $form_state, $frv = null) {
      ...
    }
    public function validateForm(array $form, FormStateInterface $form_state) {
        ...
        if (true) {
            $form_state->setRebuild();
            $frv = 'some new string';
            // somehow pass this new $frv to the buildForm ???
        }
    }
}

1 Answer 1

1

$form_state->set() will help do this,

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

  if($form_state->get('variable')){
    //do your logic
  }
}

public function validateForm(array $form, FormStateInterface $form_state) {
    ...
    $form_state->set('variable', 2);//any value


    if($form_state->get('variable')){
      $form_state->setRebuild(TRUE);
    }
}
1
  • Thanks for your answer, but i already know about using the Storage option, and it's not the answer i'm looking for, as i know there is a way to pass an argument to the buildForm
    – lordZ3d
    Commented Nov 21, 2017 at 14:43

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.