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

arithmetric’s picture

Title: programmatic validation does not seem to work » How to programmatically validate nodes for duplicate values
Component: User interface » Documentation
Status: Active » Closed (works as designed)

Hi 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.

giorgio79’s picture

Wow, thanks Joe for this in depth explanation. This was perfect. Thank you very much, will do as you say :)

ikeigenwijs’s picture

I found this really helpful, keeping track of this.