1

I have a content type with an image field. I need to validate this field because I need to check if user upload a real image or only a file with image's extension (like jpg, gif, png etc). I have tried and readed 100+ things but none resolve my problem.

I have tried via "#upload_validators" with this code:

foreach ($form['field_all_images'][$form['field_all_images']['#language']] as $k => $v) {
  if (!is_integer($k)) continue;

  $form['field_all_images'][$form['field_all_images']['#language']][$k]['#upload_validators']['check_image_correct'] = array();
}

where "check_image_correct()" is a my custom function that checks via imagegetinfo(). My problem, in this "try/case", is that the function works/is called only for the first image. I have tried to move this function from hook_form_alter() to an #after_build function, without result (only the first time it works).

0

It looks like there a couple of very small bugs in that code that would stop it working properly, try this:

$lang = $form['field_all_images']['#language'];
foreach (element_children($form['field_all_images'][$lang]) as $key) {
  $form['field_all_images'][$lang][$key]['#upload_validators'][] = 'check_image_correct';
}

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.