0

I need to validate the Image Name uploaded via Image field in Content type.How can i validate the Image name.

Any Help is Appreciated

Thanks,

1 Answer 1

3
function hook_form_alter(&$form, &$form_state, $form_id){
    if($form_id == "YOUR_FORM_ID"){
        $form['#validate'][] = 'myvalidation_function';
    }
}

function myvalidation_function($form, &$form_state) {   
    $file_fid = $form['field_your_content_type_image_field']['LANGUAGE_NONE'][0]['#value'][fid];
    $file = file_load($file_fid);
    $file_name = explode( '.', $file->filename);    
    if($file_name[0] != $form['type']['#value']){
        drupal_set_message('your validation error');
    }
}

Hope this works. I have tested on my system :)

3
  • You can also use this on node_insert or node_update. just take fid from $node object instead of $form['field_your_content_type_image_field']['LANGUAGE_NONE'][0]['#value'][fid]; and changes the hook accordinly May 9, 2013 at 11:05
  • Thanks Deepak, Can we rename the File name in the backend and store in Database
    – prashanth
    May 9, 2013 at 13:07
  • @prashanth That is another question and should be asked separately. I think this solution answers the original question you have asked; in that case you should mark it as accepted and open a separate question if you have more issues.
    – AjitS
    May 10, 2013 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.