Closed (works as designed)
Project:
Unique field
Version:
6.x-1.2
Component:
Documentation
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
26 Nov 2009 at 14:57 UTC
Updated:
25 Apr 2012 at 08:25 UTC
Hello,
I am programmatically creating nodes, with this piece of code
.....
$node = node_submit($node);
if ($node->validated)
{
node_save($node);
echo 'Node: ' . $node->title . '(' . $node->nid . ') was saved. <br />';
}
else
{
echo 'Node: ' . $node->title . '(' . $node->nid . ') was not saved. <br />';
}
...
Duplicate nodes are saved without a problem, even though I have Unique field installed. Once I go to edit a node like this in the browser, it does show that uniquness check failed and does not let save.
Comments
Comment #1
arithmetric commentedHi giorgio79,
The node_submit() function does not validate the node. As you can see from the node_submit() code, $node->validated is always set to TRUE:
http://api.drupal.org/api/function/node_submit/6
Instead, to validate a node you should use node_validate(). This will run the node's values through Unique Field's validation routine, as well as the node validation rules of Drupal core and any other modules. If validation fails, then a form error is set, which you can detect using form_get_errors().
If you want only the results from Unique Field's validation, then you could use module_invoke() to run only Unique Field's hook_nodeapi routine, and again check for errors using form_get_errors().
Hope that helps. Let me know if there is any trouble using this approach.
Comment #2
giorgio79 commentedWow, thanks Joe for this in depth explanation. This was perfect. Thank you very much, will do as you say :)
Comment #3
ikeigenwijs commentedI found this really helpful, keeping track of this.