function physical_field_validate in Physical Fields 7
Implements hook_field_validate().
File
- ./
physical.module, line 51 - Defines fields (e.g. weight and dimensions) to support describing the physical attributes of entities.
Code
function physical_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
if ($field['type'] == 'physical_volume') {
// Ensure only numeric values are entered in volume fields.
foreach ($items as $delta => &$item) {
if (!empty($item['volume']) && !is_numeric($item['volume'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'physical_volume',
'message' => t('%name: volume must be a number.', array(
'%name' => $instance['label'],
)),
);
}
}
}
elseif ($field['type'] == 'physical_weight') {
// Ensure only numeric values are entered in weight fields.
foreach ($items as $delta => &$item) {
if (!empty($item['weight']) && !is_numeric($item['weight'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'physical_weight',
'message' => t('%name: you must enter a numeric weight value.', array(
'%name' => $instance['label'],
)),
);
}
}
}
elseif ($field['type'] == 'physical_dimensions') {
// Ensure only numeric values are entered in dimension fields.
foreach ($items as $delta => &$item) {
// Keep track of which dimensions have data.
$has_data = array();
foreach (physical_dimensions() as $key => $dimension) {
// The current dimension has data if it is set and not NULL.
if (isset($item[$key]) && $item[$key] != NULL) {
$has_data[] = $key;
}
// Throw an error if the value is not numeric.
if (!empty($item[$key]) && !is_numeric($item[$key])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'physical_dimensions',
'message' => t('%name: you must enter a numeric !dimension value.', array(
'%name' => $instance['label'],
'!dimension' => drupal_strtolower($dimension['name']),
)),
'dimension' => $key,
);
}
}
// If one dimension had data, they all must. Set an error for fields that
// need data.
if (count($has_data) > 0) {
foreach (physical_dimensions() as $key => $dimension) {
if (!isset($item[$key]) || $item[$key] == NULL) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => 'physical_dimensions',
'message' => t('%name: you must supply a !dimension value.', array(
'%name' => $instance['label'],
'!dimension' => drupal_strtolower($dimension['name']),
)),
'dimension' => $key,
);
}
}
}
}
}
}