Validate callback for alt and title field, if the user wants them required.
This is separated in a validate function instead of a #required flag to avoid being validated on the process callback.
function _image_field_required_fields_validate($element, &$form_state) {
// Only do validation if the function is triggered from other places than
// the image process form.
if (!in_array('file_managed_file_submit', $form_state['triggering_element']['#submit'])) {
// If the image is not there, we do not check for empty values.
$parents = $element['#parents'];
$field = array_pop($parents);
$image_field = drupal_array_get_nested_value($form_state['input'], $parents);
// We check for the array key, so that it can be NULL (like if the user
// submits the form without using the "upload" button).
if (!array_key_exists($field, $image_field)) {
return;
}
elseif (empty($image_field[$field])) {
form_error($element, t('The field !title is required', array(
'!title' => $element['#title'],
)));
return;
}
}
}