0

I have a custom form defined by the class below :

class MyForm extends FormBase {

    /**
     * @see \Drupal\Core\Form\FormInterface::getFormId()
     */
    public function getFormId(){
        return 'my_form_id';
    }

    /**
     * @see \Drupal\Core\Form\FormInterface::buildForm()
     */
    public function buildForm( array $form, FormStateInterface $form_state ){       
        $form['my_textfield'] = array(
            '#type' => 'textfield',
            '#title' => t('My textfield'),
            '#default_value' => 'I want the current value here !',
            '#description' => t('Description of my textfield.'),
            '#required' => true
        );

        return $form;
    }

    /**
     * @see \Drupal\Core\Form\FormInterface::validateForm()
     */
    public function validateForm( array &$form, FormStateInterface $form_state ){
        // Not implemented yet...
    }

    /**
     * @see \Drupal\Core\Form\FormInterface::submitForm()
     */
    public function submitForm( array &$form, FormStateInterface $form_state ){
        // Not implemented yet...
    }

}

The form is then built in my page controller, using \Drupal::formBuilder()->getForm(). It overrides the form's #method attribute to GET because I want the field state to be present in the URL.

I'm looking for a correct Drupal-friendly way to assign the #default_value of my fields with the previously submitted state of the form.

Edit:

If I use a classic POST-method form and I add $form_state->setRebuild(); in ::submitForm(), I can get my values in ::buildForm() using $form_state->getValues(). And the form is automatically rebuilt with the previously submitted values, without having to assign the default values programmatically...


Could-be-related posts

closed as unclear what you're asking by Kevin, leymannx, DRUPWAY, kiamlaluno Jun 28 '18 at 23:33

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

0

You will have to set query parameters on submit and then you can fetch them and use as default values.


1] Redirect on submit

$form_state->setRedirect($route_name, $route_parameters, $options);

2] Fetch query parameters

$params = \Drupal::request()->query->all();
  • Why the redirection ? – Eria Jun 21 '18 at 13:54
  • I thought you want to add query parameters to the current url. It is a way to refresh the current page and append submitted values as query parameters. The solution you found (setRebuild) is very simple and practical though. – Vinayak Anivase Jun 21 '18 at 15:55
0

My final implementation...

MyForm.php

class MyForm extends FormBase {

    /**
     * @see \Drupal\Core\Form\FormInterface::getFormId()
     */
    public function getFormId(){
        return 'my_form_id';
    }

    /**
     * @see \Drupal\Core\Form\FormInterface::buildForm()
     */
    public function buildForm( array $form, FormStateInterface $form_state ){       
        $form['my_textfield'] = array(
            '#type' => 'textfield',
            '#title' => t('My textfield'),
            '#default_value' => 'I want the current value here !',
            '#description' => t('Description of my textfield.'),
            '#required' => true
        );

        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = [
           '#type' => 'submit',
           '#value' => $this->t('Submit'),
           '#button_type' => 'primary',
        ];

        return $form;
    }

    /**
     * @see \Drupal\Core\Form\FormInterface::submitForm()
     */
    public function submitForm( array &$form, FormStateInterface $form_state ){
        // Nothing to do here...
    }

}

MyController.php

class MyController extends ControllerBase {

    public function index( Request $request ){

        // Here I can use $request->get('my_textfield') if needed

        $form_state = new FormState();
        $form_state->setRebuild(); // So that the form is rebuilt with previously submitted values
        $form_state->setMethod('GET'); // So that the form is submitted via GET method (URL)

        return \Drupal::formBuilder()->buildForm( '\Drupal\my_module\Form\MyForm', $form_state );
    }

}

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