我正在尝试验证一组节点,然后再将它们保存到数据库中:
foreach ($nodes_to_save as $node) {
if (! (node_validate($node, $form))) {
form_set_error('', t('Node %title did not validate. No nodes were saved.', array('%title' => $node->title)));
$success = FALSE;
break;
}
}
node_validate的文档说它将调用form_set_error()
来指示节点未验证的原因。但是,上述代码不会发生这种情况。我得到的只是我手动设置的错误。我做错了什么?
答案 0 :(得分:1)
这不会更有用吗?
$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;
}
}
而不是将$success
作为FALSE
返回给通过循环运行的每个节点?
答案 1 :(得分:0)
试试这个
$form
应该是一个数组!
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;
}
}
或者不要使用if beause node_validate doesnt
return true
或false
;
foreach ($nodes_to_save as $node)
{
node_validate($node, $form);
$success = FALSE;
return $success;
}
}
请注意,node_validate将第一个参数作为对象,第二个参数作为数组