Form submit button with ajax, replace form with another one (error 500) 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' =&gt; 'textfield',
            '#title' =&gt; t('Title*:'),
            '#required' =&gt; TRUE
        );
        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = array(
            '#type' =&gt; 'submit',
            '#value' =&gt; $this-&gt;t('Add'),
            '#button_type' =&gt; 'primary',
        '#ajax' =&gt; [
            'wrapper' =&gt; 'projekt-form-ajax-id',
            'callback' =&gt; 'form_ajax_submit',
            'method' =&gt; 'replace',
            'effect' =&gt; 'fade'
        ]
        );
        $form['#prefix'] = '&lt;div id="projekt-form-wrapper"&gt;';
        $form['#suffix'] = '&lt;/div&gt;';
        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:


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).
You need to add your namespaced class to the callback property.
For example:

'callback' =&gt; 'Drupal\mymodule\Form\ProjektForm::form_ajax_submit'


or  

'callback' =&gt; [$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!