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.
Comments
Comment #1
markus_petrux commentedTo 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.
Comment #2
mercmobily commentedHello,
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.
Comment #3
markus_petrux commentedWell, 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:
I my case node_validate() is called.
If you can't see that, hmm... have you checked admin/logs ?
Comment #4
mercmobily commentedOK, 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.
Comment #5
markus_petrux commentedSince 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.
Comment #6
mercmobily commentedHello,
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.
Comment #7
mercmobily commentedFOUND IT!!!
The problem is in node_article_form().
Right now, it returns:
return array_merge($form, filter_form($node->format));
This seems to inhibit node_article_validate() - I have no idea why.
If I return:
return $form;
Then the validation happens.
Any idea what filter_form does? And should it be deleted from node_example?
Comment #8
drewish commentedmercmobily, based on that last bit of code you pasted in, it doesn't look like you've got a HEAD version of the node_example.module. take a look around in it and see if it implements hook_nodeapi()'s "'delete revision" operation. if it doesn't you've got an old version.
i've heard that drupaldocs.org hasn't been updated recently, and that it's being moved to a different server, so you'll probably have to use CVS to get the current version. at the time of this posting, this is the current version.
hopefully that will help fix a couple of your problems.
Comment #9
mercmobily commentedHi,
OUCH!
That's really bad. I based my module on:
http://drupaldocs.org/api/head/file/contributions/docs/developer/example...
Whici is ponted to by:
http://drupaldocs.org/api/head
Now... the link you gave me is the direct CVS account. I had assumed that the two were synchronised! :-|
Merc.
Comment #10
mercmobily commentedBloody hell guys...
I am slow at programming in PHP and Drupal. But... honestly, I wasted *hours* because if the synchronisation problem betweeb the link in the documentation and the "real" CVS.
Please please please fix it. Porting the module from the "oid" style to the new style is a major pain.
Merc.
Comment #11
sepeck commentedHere's the problem. Drupaldocs is not controlled by members of the documentation team. We may have to setup a seperate project for issues with api.drupal.org when the api docs are transferred there.
Not sure where to put this issue at this point.
Comment #12
mercmobily commentedHi,
Is it possible to transfer this bug to whatever project has people who are actually in control api.drupal.org?
The old node_example module there is a royal pain, and a huge waste of time and efforts for anybody who tries to develop a new module...!
Merc.
Comment #13
mercmobily commentedClosed.