Drupal filefield errors on node forms

For a recent client, I needed a node-add form in a custom path. Unfortunately, loading the form there via code (in order to prepopulate the node object) lead to all sorts of FileField errors, and the solution proved to be extraordinarily simple.

I didn't want the custom menu item (created in code) to lead directly to the node form, but to create a node object, populate some values based on business logic, and show the form.

Which worked just fine - but when uplaoding a field, errors popped up, making it a blocking issue.

The solution is simple - fielfield needs to have node pages included in order to work as expected, something which I did in my custom code for loading the add page, but not on form_alter, which is what the AJAX for uploading files uses.

Thus, the only thing needed is to add the following line :

form_load_include($form_state, 'inc', 'node', 'node.pages');

in your implementation of form_alter, and all the errors magically dissapear!