Ajax'ed file field doesn't find the form constructor function on rebuild I'm manually printing the user profile form in a block using:

module_load_include('inc', 'user', 'user.pages');
$form = drupal_get_form('user_profile_form', $user);


That works great for displaying and submitting the form.

The form has an image field to add a picture. When I try to upload the picture through Ajax, I get a PHP notice that drupal_retrieve_form() is missing the form constructor function (user_profile_form()), and the image field isn't being shown anymore.

I'm guessing the image field's Ajax doesn't know about the user.pages file that I included, and can't find the form.

Any idea how to fix this?
You will need to include file by using the $form_state['build_info']['files'] array.

For example:

$form_state['build_info']['files'][] = drupal_get_path('module', 'user') . '/user.pages.inc';