Closed (fixed)
Project:
Field Validation
Version:
7.x-2.3
Component:
Code
Priority:
Major
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
29 Jan 2013 at 10:19 UTC
Updated:
1 Apr 2013 at 09:19 UTC
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
Comment #1
razkovnik commentedComment #2
razkovnik commentedI've figured it out far more easily:
Comment #3
razkovnik commentedComment #4
jason.fisher commentedYou may want to use the 'unique' module for this case instead.
Comment #4.0
jason.fisher commentedPut more details and code for comments.