As per my testing and the documentation at http://api.drupal.org/api/function/hook_validate/6, the $node argument is not passed by reference.

From http://api.drupal.org/api/function/hook_validate/6 :
hook_validate($node, &$form)

The http://api.drupal.org/api/function/node_example_validate code example uses &$node and even goes as far as demonstrating how you can modify $node as needed. I believe this is simply not possible.

CommentFileSizeAuthor
#2 node_example_hook_validate.patch593 bytesilo

Comments

jhodgdon’s picture

Title: Documentation problem with node_example_validate » node_example_validate() should not try to modify $node
Project: Drupal core » Examples for Developers
Version: 6.x-dev » 6.x-1.x-dev
Component: documentation » Code

Just to clarify, in PHP 4, the called function determines whether an object is passed by value or reference (in PHP5, it is always by reference). In other words, if I make an example_validate() function and put &$node as the first argument, then that $node will be passed in by reference.

However, as noted in the hook_validate documentation, "Changes made to the $node object within a hook_validate() function will have no effect." (this is because the $node object that is passed in, by reference or not, is not used after that in the calling function).

So you are correct that the node_example_validate() function is not going to work when it sets the quantity to zero. It should be following the instructions in hook_validate()'s doc and using the form state to change the value.

Moving to correct issue queue (we are maintaining the example modules now in a separate project).

ilo’s picture

Status: Active » Needs review
StatusFileSize
new593 bytes

The form sets empty value as 0 and I was about to introduce a check for $node->quantity no hook_insert and hook_update, but this is the purpose of hook_validate, so I just removed the parameter by reference and the code modifying $node->quantity.

ilo’s picture

Status: Needs review » Fixed

committed to DRUPAL-6--1.

jhodgdon’s picture

Status: Fixed » Needs review

It might be useful for the example to show how to set a new value for something using the form state, as an illustration.

ilo’s picture

Sorry, jhodgdon, I can't follow you. It is the $form, not the $form_state what is passed to hook_validate. Do you have something in mind? can you point me an example of this form change?

jhodgdon’s picture

Status: Needs review » Fixed

True about $form... although is $form_state part of $form? If not, then the advice here http://api.drupal.org/api/function/hook_validate/6 about calling form_set_value() is useless.

Our status changes crossed paths...

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.