To reproduce:

create a simple node type, or use an existing on (page, story should work) Add this code to hook_validate so you can see what's going on:

drupal_set_message("In hook_validate, title is $node->title");

Enter a title, "foo".

Hit Preview

What happens behind the scenes is

1) hook_validate is called without taking into account $POST['edit']. That is, title is null.
2) hook_validate is not called with title= foo. More importantly, no values from the form are validated.

The problem starts with this call to node_preview (in node.module):

  if ($op == t('Preview')) {
    $form['node_preview'] = array('#value' => node_preview(array2object($_POST['edit'])), '#weight' => -100);
  }

While node_preview tries to validate the "node" created from $_POST['edit'], it never does, because the node has no type (it can't determine which module to invoke hook_validate).

One fix might be to change:

  $form['type']    = array('#type' => 'value', '#value' => $node->type);

into:

  $form['type']    = array('#type' => 'hidden', '#value' => $node->type);

This would set $_POST['edit']['type'] and cause node_preview to actually call hook_validate. If you try this, you'll see hook_validate is called twice. The first call should not occur, IMHO, but that's another issue I guess.

I suspect some others of these should be hidden fields and not just form values, for the same reason:
(see node.module)

  /**
   * Basic node information.
   * These elements are just values so they are not even sent to the client.
   */
  $form['nid']     = array('#type' => 'value', '#value' => $node->nid);
  $form['vid']     = array('#type' => 'value', '#value' => $node->vid);
  $form['uid']     = array('#type' => 'value', '#value' => $node->uid);
  $form['created'] = array('#type' => 'value', '#value' => $node->created);
  $form['changed'] = array('#type' => 'value', '#value' => $node->changed);
  $form['type']    = array('#type' => 'hidden', '#value' => $node->type);

CommentFileSizeAuthor
#11 forms4_0.patch11.85 KBasimmonds
#10 forms4.patch11.2 KBchx
#7 node_cycle_0.patch10.12 KBchx
#4 node_cycle.patch10.12 KBchx
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

chx’s picture

Over my dead body change any of those into hidden. I'll create a real fix. Stay tuned.

Dave Cohen’s picture

Perhaps simply an array_merge with $_POST['edit']? I'm sure there are a number of ways.

Glad you're on it,

-Dave

chx’s picture

Title: nodes not validated on preview » Forms API breaks node submission
Assigned: Unassigned » chx
Priority: Normal » Critical
chx’s picture

Status: Active » Needs review
FileSize
10.12 KB

Adrian has added some tree fixes, Steef has added some url() calls.

The major and relevant stuff is that I changed how the node form is built, now it's two pass. This was we actually have the relevant but only the relevant node info.

Will check the concept with poll. Please review.

gtcaz’s picture

Site will not load after applying this patch on a PHP5.0.4 install. Trying to dig into the problem now. Not getting any error messages...

moggy’s picture

just getting blank pages with this patch too.

BTW running PHP 4.3.8

chx’s picture

FileSize
10.12 KB

D'oh. How did this version of form.inc got into a patch?? There was a ) missing.

asimmonds’s picture

Testing the patch:
Missing ')' line #58 in form.inc
admin/access/permissions save fails, something wrong with the form tree, as there is no longer a checkboxes array
admin/node page errors with: Invalid argument supplied for foreach() in includes\form.inc on line 240
admin/themes save disables all themes

but on top of that, node preview now works...

chx’s picture

Finally! Someone who knows what's a review :) Adrian has played with the trees, I was playing with the node stuff. Looking into that.

chx’s picture

Title: Forms API breaks node submission » Forms API fixes #4: node submission, trees
FileSize
11.2 KB

Please review. I fixed what asimmonds found. An "it is broken" is not a review. Thanks.

asimmonds’s picture

FileSize
11.85 KB

I have updated the patch, correcting a value to #value in node.module and some spacing.

Node preview and submission work.
The problems I found in #8 all test OK now.

chx’s picture

Status: Needs review » Needs work

hunmonk says that adrian has some cool stuff on Benton and I'll need to check with him. Stay tuned.

Please do not change the status until then.

chx’s picture

Status: Needs work » Fixed
Anonymous’s picture

Status: Fixed » Closed (fixed)