1

I have added a custom button which has an ajax callback with the following code to the user profile form.

$form['validate_2'] = array(
          '#type' => 'button',
          '#value' => t('validate 2'),
          '#ajax' => array(
            'callback' => 'example_validate',
            'wrapper' => 'validate-settings',
            'method' => 'replace'
          ),
          '#name' => 'user_validate_2',
          '#id' => 'user-validate-2',
          '#weight' => 10
        );

But when I click on it, it gives me ajax error in popup saying

An AJAX HTTP request terminated abnormally.
Debugging information follows.
Path: /system/ajax
StatusText: n/a
ResponseText: 
( ! ) Fatal error: Call to undefined function user_profile_form_validate() in /includes/form.inc on line 1520

In my ajax callback I'm simply displaying the $form_state array. Any idea what I'm doing wrong?

The form's own submit button is not with ajax.

function example_validate($form, &$form_state) {
dsm($form_state);
}
5
  • in a custom module
    – Ahmad
    Commented Feb 10, 2017 at 7:45
  • yes in hook_form_alter in .module file
    – Ahmad
    Commented Feb 10, 2017 at 7:49
  • Can you add your callback function "example_validate" code to your question?
    – user71549
    Commented Feb 10, 2017 at 9:19
  • added the callback function.
    – Ahmad
    Commented Feb 10, 2017 at 9:23
  • I have tried the same and it is working fine for me. I don't find any such issue. You can try what @Clive have suggested.
    – user71549
    Commented Feb 10, 2017 at 9:40

1 Answer 1

1

If Drupal can't find one of its core files it's nearly always because you're trying to use it in a context in which it wouldn't be automatically included.

For example when you use the user profile form normally, the menu item makes sure the file containing the form functions is included.

For your example the missing function is in user.pages.inc, so:

form_load_include($form_state, 'inc', 'user', 'user.pages');
4
  • but why it is calling that function. It should call only the callback function with $form_state array. is it possible? I think the button validate the whole form.
    – Ahmad
    Commented Feb 10, 2017 at 9:13
  • That's correct - you can use #limit_validation_errors to control it though
    – Clive
    Commented Feb 10, 2017 at 9:47
  • In the ajax callback the $form_state does not contains the updated values. It has the old values.
    – Ahmad
    Commented Feb 10, 2017 at 14:50
  • Check the rest of your code/contrib modules in that case, that's not what should happen in normal circumstances
    – Clive
    Commented Feb 10, 2017 at 14:53

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.