Here’s a code snippet that ensures the submitted title is unique across all nodes.
<?php
function MYMODULE_form_alter ( $form_id , $form ) {
if ( substr ( $form_id , - 10 ) == '_node_form' ) {
// add custom form validation function
$form [ '#validate' ] = array_merge ( array ( '_MYMODULE_helper_validate' => array ()), $form [ '#validate' ]);
}
}
function _MYMODULE_helper_validate ( $form_id , $form_values , $form ) {
// ensure title is unique
if ( strlen ( $form_values [ 'title' ])) {
// check for unique title
$sql = "select distinct title from { node } where 1=1 " ;
// don't want to include current title
if ( $form_values [ 'nid' ]) $sql . = "and nid != '" . db_escape_string ( $form_values [ 'nid' ]) . "'" ;
$resource = db_query ( $sql );
$titles = array ();
while ( $result = db_fetch_array ( $resource )) $titles [] = $result [ 'title' ];
if ( in_array ( $form_values [ 'title' ], $titles ))
form_set_error ( 'title' , $form [ 'title' ][ '#title' ] . ' has already been submitted.' );
}
}
?>