1

I've found the post How to display one of Drupal's default forms which talks specifically about this issue. however I wanted to post comments on it to get more info, It didn't seem to let me. Mabey I missed a button somewhere.

Anyway, I have section of my Drupal 7 site for all account and user stuff. /my/ This is a portal so to speak, /my/account is a menu handler that originally called drupal_get_form('user_profile_form', $user); Everything works, except when trying to upload, My profile is customised with a field to upload a file and field to upload an image.

Neither field works, it just returns errors. after the ajax kicks in.

Using the method found in the above linked post my entire form refuses to save and gives me validation errors. Thew above link mentions that custom callbacks and/or validators may have to be added due to referencing issue so i was hoping that someone could point me in the direction of what those custom things are.

Thanks.

1 Answer 1

3

You're likely using module_load_include() to include modules/user/user.pages.inc.

However, you need to use form_load_include() whenever you attempt to load, build, process, and render a form that is defined and constructed in an entirely different include file.

form_load_include() requires to pass the $form_state in order to record the file to include (and automatically load it later, if needed). This needs to happen before the form structure is cached (which happens directly after invoking the constructor).

In order to do that, you can resemble what drupal_get_form() does, and inject the needed form_load_include() prior to building the form, like this:

$form_id = 'user_profile_form';
$form_state = array();
$form_state['build_info']['args'] = array($user);

// Load user.pages.inc and ensure it is automatically reloaded if
// this form is rebuilt via AJAX or other means.
form_load_include($form_state, 'inc', 'user', 'user.pages');

$form = drupal_build_form($form_id, $form_state);

More details:

Normally, Form API automatically takes over the include file to load (if any) from the menu router item definition in hook_menu().

In your case, your custom 'my/account' menu router item is not directly related to user.module, nor user.pages.inc, so the system does not know that this include file is required to build the form, especially when the form is submitted and rebuilt via AJAX — which happens on yet another system path/menu router item, namely 'system/ajax' (which, by default, would only know about an include file of your 'my/account' menu router item, if any).

In turn, Form API gets a request to rebuild user_profile_form, but the form constructor function user_profile_form() is not defined, because user.pages.inc was not loaded, since the cached $form_state does not contain a pointer that the file needs to be loaded.

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.