<div>Hi all,<br><br>For the node_import module which tries to import a whole set of nodes from a CSV file, I have the following problem: how does one validate a "$node" before "node_save()"ing it?<br><br>
Let me explain:
<br><br>In 4.6 we had the "node_validate($node)" function which validates the node (calling "hook_validate()" and "hook_nodeapi('validate')" as needed). If this was succesful (no "form_get_errors()"), then we could safely "node_save($node)" it.
<br><br>Now, in 4.7, most (if not all) validation is done using the form API, in things like "#validate" and "#required". For example, "story.module" requires that the title is not empty. It does that by setting a "#required" on the title-textfield. If I do:
<br>$node = (object) array('type' => 'story', 'uid' => 42, 'title' => '');<br>the node will pass "node_validate($node)", but this is because the validation inside "$form" was not run!<br><br>
So my questions are:
<br>- how does one validate a node programmatically *correctly*?<br>- what is the point of the "node_validate()" function and the hooks if it doesn't actually *validate* the node at all? At least rename it to "node_noop()" so things are clear.
<br>- really, why have "node_validate()" at all? Does anyone do anything usefull in it?<br>- and the real question: how does the following sequence of
4.6 translate into 4.7:<br><pre>$node->type = 'story';<br>$node->uid = 1;<br>$node->title = ''; // invalid empty title!<br>$node->body = ''; // invalid empty body!<br><br>$node = node_validate($node); // if life was only that
<br> // that easy in 4.7... <br><br>if (form_get_errors()) {<br> // we detected the error... do something<br> unset($GLOBALS['form']);<br><br> // note: we unset $GLOBALS['form'] so the next time we
<br> // try this trick, form_get_errors() return the errors<br> // of the last run and not of the previous.<br> // If something like this would be possible in 4.7 it<br> // would be great, but form_set_error() in 4.7 saves the
<br> // errors in a static variable inside form_set_error()<br> // instead of in $GLOBALS['form']. I see no way to unset<br> // that...<br> $output .= node_view($node);<br>}<br>else {<br> // no errors? hmm, great, save it... quickly before
<br> // "they" introduce something else that breaks<br> // "common sense". Really: why call something<br> // foobar_validate() if it doesn't validate anything<br> // at all... Am I alone in this? I guess so.
<br> node_save($node);<br>}<br><br></pre>Anyway, I have a solution, but that is so hackerish that I hope others know "The Right Solution(tm)".<br><br>Thanks,<br></div><div><span class="sg">Robrecht<br>
</span></div>