Thank you for the great module!
I want to validate a field input in a node edit form against values already stored for this field in the database. The field is a nodereference.
So I use this php code:

/**
*Implement a function to get the NID of the currently edited node.
**/
function node_get_id(){
if(arg(0) == 'node' && is_numeric(arg(1))){
$nid = arg(1);
$node = node_load(array('nid'=> $nid));
   }
}
/**
*Implement a function to find the ID of the referenced node 
*by the nodereference field in the currently edited node  
**/
function field_get_id() {
$currentnodeid = node_get_id();
$query = db_select('field_data_field_period_1', 'f')
    ->fields('f', 'field_period_1_nid')
    ->condition('entity_id', $currentnodeid, '=')
    ->execute()
    ->fetchField();
}
/**
*Implement a function to find the TITLE of the referenced node, 
*which is the actual value of the validated field   
**/
function field_get_title() {
$reffieldid = field_get_id();
$query = db_select('node', 'n')
    ->fields('f', 'title')
    ->condition('nid', $reffieldid, '=')
    ->execute()
    ->fetchField();
}
/**
*Function to validate the new input value against the one stored in the database 
*and set error if there is already a value and it is different from the new user input
**/
function validate_input_value() {
$fieldcurrvalue = field_get_title();
if(isset($fieldcurrvalue)&&($this->value != $fieldcurrvalue)){
  $this->set_error();
}
}

But nothing happens at all. I wonder what my mistake is?

Comments

razkovnik’s picture

Assigned: razkovnik » Unassigned
Category: task » support
razkovnik’s picture

I've figured it out far more easily:

function my_module_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  $titles = array();
  $field_id = $form_state['values']['field_id'];
  $entities = entity_load('node', $form_state['values']['entity_ids'], $conditions = array(), $reset = FALSE);
  
  foreach ($entities AS $entity) {
    if (count($entity -> $field_id)) {
      $titles[] = $entity -> title;
    }
  }
  
  if (count($titles)) {
    hide($form[$field_id]);
    hide($form['submit']);
    drupal_set_message(t('Inappropriate <strong>@titles</strong> - contains value.', array('@titles' => implode(', ', $titles))), 'error');
  }
razkovnik’s picture

Status: Active » Closed (fixed)
jason.fisher’s picture

You may want to use the 'unique' module for this case instead.

jason.fisher’s picture

Issue summary: View changes

Put more details and code for comments.