-1

I am using node gallery module.While creating an album, when the user enters duplicate name then error message is displayed as

This form requires that the fields Album name, Author are a unique combination. The specified values are already used.

I want to change this error message for duplicate album name. I have created a hook for this.

function hook_form_alter(&$form, &$form_state, $form_id) {
  if ('NODE form id' == $form_id) {
   $form['#validate'][0] = 'my_custom_validate';
  }
}

function my_custom_validate(&$form, &$form_state){

  //Add code to check the title using db_query
  $title = $form_state['values']['title'];


     //$fetch data from node table w.r.t this $title 

  //if title exists
  form_set_error('title','custom message');
} 

Here how can I compare new node title with already entered nodes in database?

2 Answers 2

1

Use an EntityFieldQuery:

$query = new \EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
  ->propertyCondition('type', 'some_type')
  ->propertyCondition('title', $title);

$result = $query->execute();
if (!empty($result['node'])) {
  // Node with provided title already exists.
}
0

You can use http://drupal.org/project/unique_field module. It performs additional validation when a node is created or updated by a user to require that a node's title or other specified fields are unique. Besides that

try this code may be helpful to you:

function my_custom_validate(&$form, &$form_state){

  //Add code to check the title using db_query
  $title = $form_state['values']['title'];

  $node_title = NULL;
  //$fetch data from node table w.r.t this $title
  $node_title = db_query('SELECT title FROM node WHERE title=:title',
        array(':title'=>$title))->fetchField();

  if (!empty($node_title )) {

    //if title exists
     form_set_error('title','custom message');
 }
} 
2
  • I worked with the module but custom error message is displaying even though the album title is unique.
    – 12345
    Commented Aug 11, 2014 at 12:44
  • Try the above code may be helpful to you.
    – Rahul Seth
    Commented Aug 11, 2014 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.