I'm trying to validate a set of nodes before saving them to the database:
foreach ($nodes_to_save as $node) {
if (! (node_validate($node, $form))) {
form_set开发者_StackOverflow_error('', t('Node %title did not validate. No nodes were saved.', array('%title' => $node->title)));
$success = FALSE;
break;
}
}
The documentation for node_validate says that it will call form_set_error()
to indicate why the node hasn't validated. However, this does not happen with the above code. All I get is the error I set manually. What am I doing wrong?
Wouldn't this be more useful?
$success = true;
foreach ($nodes_to_save as $node){
node_validate($node);
//returns null or array
$errors = form_get_errors();
//if there was an error, send an extra message indicating the node that was not saved
if($errors){
form_set_error('', t('Node %title did not validate. It was not saved.', array('%title' => $node->title)));
$success = false;
break;
}
}
Instead of returning $success
as FALSE
for every single node that is run through the loop?
try this
the $form
should be an array !
foreach ($nodes_to_save as $node)
{
if (! (node_validate($node, $form)))
{
form_set_error('', t('Node %title did not validate. No nodes were saved.', &drupal_static(__FUNCTION__));
$success = FALSE;
return $success;
}
}
Or dont use the if beause node_validate doesnt
return true
or false
;
foreach ($nodes_to_save as $node)
{
node_validate($node, $form);
$success = FALSE;
return $success;
}
}
note that node_validate takes as first parameter an object and as second parameter an array
精彩评论