I want a date field to be unique per user so they can't add two nodes with the same date. I have Unique Field installed. Unfortunatly, they don't offer user scope.

Anyone know how to achieve this?


I don't know the Unique Field module, but I have a suggestion instead.

Create a little utility module which alters the node form of that node type and adds a custom validation. That validation could check for anything on the form, date inclusive.

In the Drupal 6 days it worked like this. I hope this is enough for a starting point. [Also, reading the book Pro Drupal 7 Development can help you a lot if you haven't written a module yet.]

  • 1
    I've created a basic module before. Basically, I would have to check in the database if the value of my date field is already present for the user? I'll start making the date field required with a custom module and then continue from there. Thanks alot for this starting point. – davidgmar Feb 12 '12 at 23:33
  • "Basically, I would have to check in the database if the value of my date field is already present for the user?" - yes, that's the validation criteria. You're welcome! :) – Scorchio Feb 12 '12 at 23:47

I would like to thank Scorchio for pointing me in the right direction. I managed to achieve what I wanted putting this code in a custom module :

function node_calendar_form_alter(&$form, &$form_state, $form_id) //alter the form
  if($form_id == 'training_node_form') //specify form with form id
    $form['#validate'][] = 'my_custom_validate_function'; //declare a custom validation function


function my_custom_validate_function($form, &$form_state)
    global $user; //because I needed the uid
    $query = new EntityFieldQuery();
    $dates_array = array(); //this will contain the dates to check for

    ->entityCondition('entity_type', 'node', '=')
    ->propertyCondition('type', 'training')
    ->propertyCondition('uid', $user->uid); //my query, checks for content type and current uid

    $trainings_query = $query->execute();

    if ($trainings_query) { //if query is successful
       $session_nodes = node_load_multiple(array_keys($trainings_query['node'])); //load the nodes
       foreach ($session_nodes as $training) {
           array_push($dates_array, substr($training->field_training_date['und'][0]['value'], 0, -9)); //push the dates in $dates_array after trimming the 00:00:00
    if (in_array(substr($form_state['values']['field_training_date']['und']['0']['value'], 0, -9), $dates_array)) { //if the date is already used
       form_set_error('field_training_date', t("Vous avez déjà un entrainement pour cette date. Veuillez modifier l'entrainement ou pour éviter de la duplication de contenu.")); //throw in error

I commented my code for anyone who would like to do something like this in the future.


The Drupal module https://drupal.org/project/field_validation does this. Just add a "Unique values" validator to the field and set the scope to "entity" or "bundle". There is then an option for "Per user" uniqueness across the field.


The user scope functionality you are asking for is available in Unique Field Module by default. There is an option called "Choose whether values must be unique individually or in combination". Select "The combination of values from the specified fields must be unique" option. And for the fields select your required field (date field in your case) along with the author field.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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