[documentation] [bug] node_validate() is never called

mercmobily drupal-docs at drupal.org
Mon Mar 13 02:35:07 UTC 2006


Issue status update for 
http://drupal.org/node/53639
Post a follow up: 
http://drupal.org/project/comments/add/53639

 Project:      Documentation
 Version:      <none>
 Component:    Developer Guide
 Category:     bug reports
 Priority:     normal
 Assigned to:  mercmobily
 Reported by:  mercmobily
 Updated by:   mercmobily
 Status:       active

Hello,


Well, since the problem seems to be in node_example, I guess it is a
documentation issue.


However, it's very frustrating: there seem to be a 1:1 correspondence
between the functions in page.module and node_example.module, and yet
page_validate() is called, and node_example_validate() isn't.


Any ideas, anybody?


Merc.




mercmobily



Previous comments:
------------------------------------------------------------------------

Sun, 12 Mar 2006 10:55:02 +0000 : mercmobily

Hi,


I developed my own module type, article_node.
I wrote my own node_article_validate(&$node)  function:


function node_article_validate(&$node) {
  if (!user_load(array('name' => $node->editor_uid_name))) {
      form_set_error('editor_uid_name', t('The editor must be a valid
account'));
  }
}


...but it wouldn't get called.
I then added this to node.module ("the" node.module which comes with
Drupal):


/** * Perform validation checks on the given node. */


function node_validate($node) {
  // Convert the node to an object, if necessary.
  $node = (object)$node;
  this_does_not_exist();  // SEE THIS! THIS WAS ADDED!
  // Make sure the body has the minimum number of words.
  [...]


Well, believe it or not, EVERYTHING works absolutely fine! Add a node,
edit it... and no error is given for calling "this_does_not_exist".


I marked it as "critical", because no validation can be really bad...


Merc.




------------------------------------------------------------------------

Sun, 12 Mar 2006 13:32:40 +0000 : markus_petrux

To use hook_validate() you have to implement hook_node_info() too. See
the page.module for an example.


>> node_validate() not being invoked?
Well, I have made the following test.
1) Goto admin/settings/content-types/page
2) Set a minimum of 10 words for page nodes and save.
3) Goto node/add/page, enter a title 'test' and a body 'just 3 words'


I get an error message issued from node_validate().


I'm changing the status of this issue to 'support request' with
'normal' priority. But maybe I didn't got the problem right, so feel
free to correct me if I've been wrong.




------------------------------------------------------------------------

Sun, 12 Mar 2006 14:55:23 +0000 : mercmobily

Hello,


I have most definitely implemented node_info (in my case,
node_article_node_info() ).
The piece of code I provided was only a tiny fraction of my module
(which I will release when it's finished, BTW).


Plus, the issue was marked as critical because you can write *jokes* in
node_validate() in the CORE module node.module - it is never, ever
executed.


As I wrote above, change the core function node.module from:


------------------------
/** * Perform validation checks on the given node. */


function node_validate($node) {
// Convert the node to an object, if necessary.
$node = (object)$node;
// Make sure the body has the minimum number of words.
[...]
------------------------


To:


------------------------
/** * Perform validation checks on the given node. */


function node_validate($node) {
// Convert the node to an object, if necessary.
$node = (object)$node;


knock_knock_who_is_it_its_the_doctor_doctor_who_ah_you_said_it(); 


// Make sure the body has the minimum number of words.
[...]
-------------------------


Then, you can submit forms in the system - the node_validate is never,
ever called.


I don't know Drupal well enough yet (I've installed it a week ago for
the first time), but I have the feeling that the problem I am having is
related. 


I remember seeing a patch to avoid node_validate() being called
twice... maybe that went funny?


Bye,


Merc.




------------------------------------------------------------------------

Sun, 12 Mar 2006 15:28:40 +0000 : markus_petrux

Well, I edited my version (HEAD) of node.module to call a non-defined
function from node_validate() and..


1) Goto node/add/page
2) enter a title 'test' and a body 'hello world'
3) hit 'preview'


I got a blank page with the following:


Fatal error: Call to undefined function: test_a_non_defined_function()
in /path-to-drupal/modules/node.module on line 1608


I my case node_validate() is called.


If you can't see that, hmm... have you checked admin/logs ?




------------------------------------------------------------------------

Mon, 13 Mar 2006 01:52:26 +0000 : mercmobily

OK, you are completely right and I am a complete idiot.


Well... not 100% complete idiot.
I am not sure this is a problem with the "drupal core" - I am leaving
it here just in case it is.


I based my module on node_example.module. Now, I:


* Copied module.example *as is* and put it in "modules". I also created
its table
* I changed it slightly:


-------------------
function node_example_validate(&$node) { 


 this_does_not_exist(); // I ADDED THIS


  if ($node->quantity) {
    if (!is_numeric($node->quantity)) {
      form_set_error('quantity', t('The quantity must be a number.'));
    }
  }
---------------------


The node_example_validate hook is never called - I can create
"node_example" node types without any trouble.


Now, there are two possibilities:


* The node_example.module is somehow wrong
* The drupal core is doing something wrong


To be honest, I don't think it's the core - I tested blog.module, and
blog_validate() is indeed called.


However, I think it's worth investigating, because potentially a lot of
people will use module_example.module and will run into the same
issue...!


Thanks a million,


Merc.




------------------------------------------------------------------------

Mon, 13 Mar 2006 02:21:51 +0000 : markus_petrux

Since core is changing everyday, node_example is probably out dated; I
think this issue belongs to the Document project.


In the meantime, try to look at how core modules (maybe page or story)
or some other contrib modules work.






More information about the documentation mailing list