1

I am trying to turn my Form into multi-step ajax form.

I have:

class ProjektForm extends FormBase {

    public function getFormId() { return 'projekt_form'; }


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

        $form['title'] = array(
            '#type' => 'textfield',
            '#title' => t('Title*:'),
            '#required' => TRUE
        );
        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => $this->t('Add'),
            '#button_type' => 'primary',
        '#ajax' => [
            'wrapper' => 'projekt-form-ajax-id',
            'callback' => 'form_ajax_submit',
            'method' => 'replace',
            'effect' => 'fade'
        ]
        );
        $form['#prefix'] = '<div id="projekt-form-wrapper">';
        $form['#suffix'] = '</div>';
        return $form;

    }

    public function form_ajax_submit($form, $form_state) {
        return 'OK';
    }
...

But when I click on "Add" (submit) button and see the "please wait" Drupal icon for a while I eventually get 500 error for my ajax request: enter image description here

Should I register somehow form_ajax_submit? Note that it's member function of ProjektForm. And I use ProjektForm inside my custom module which displays it from block.

What I want to do is to send the data from form via AJAX (and here I get error 500), validate it and display next form (via ajax, replacing the previous one without page reload).

3

You need to add your namespaced class to the callback property.
For example:

'callback' => 'Drupal\mymodule\Form\ProjektForm::form_ajax_submit'

or

'callback' => [$this, 'form_ajax_submit']

You also need to make sure the wrapper and the prefix html id are the same.

Finally, after you do all that, do check your logs for more details on the error that caused the 500 response.

Good luck!

| improve this answer | |
  • Thank you. What is strange, when I changed the callback as you propoused and cleared the cache... my form no longer works on ajax and it just sends itself normal way (with page refresh). How is that even possible? – PolGraphic Jan 23 '17 at 23:17
  • And the earlier calls logged "Strict warning: call_user_func_array() expects parameter 1 to be a valid callback, non-static method ". Which looks like they prevent me from using member of form for callback? – PolGraphic Jan 23 '17 at 23:19
  • 1
    projekt-form-ajax-id <> projekt-form-wrapper – Stefanos Petrakis Jan 23 '17 at 23:23
  • Thank you! I've changed the names to match each other. On Firefox I have non-ajax form now (eh... I will leave it for now), on Chrome I have ajax form with 500 error and no errors in Drupal log after I've changed form_ajax_submit to static. – PolGraphic Jan 23 '17 at 23:26
  • Ur welcome! You should use public, not static. – Stefanos Petrakis Jan 23 '17 at 23:29

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.