2

I am trying to do jquery validation on file upload for file is select or not. I want when user click on upload before submit check file is selected or not. if not stop ajax request. Any Idea to validate it?

$videoForm['profile_photos']['images']['field_photo'] = array(
    '#parents' => array('video', 'field_photo'),
    '#id' => 'field_photo',
    '#type' => 'managed_file',
    '#title' => 'Photo <em>(optional)</em>',
    '#field_name' => 'field_photo',
    '#process' => array(
    'm_customer_profile_image_remove',
    'm_customer_profile_image_upload',          
    'file_managed_file_process',
        'customer_profile_image_preview',
    ),
    '#upload_location' => 'public://' . $user->uid,
    '#limit_validation_errors' => array(array('video', 'field_photo')),
    '#upload_validators' => array(
        'file_validate_size' => array(64*1024*1024),
        'file_validate_extensions' => array('gif jpeg jpg png')
    )
);
1
  • What have you tried so far? If your current code has problems you can post it here and people might be able to point out any problem areas.
    – rooby
    Dec 10 '14 at 21:28
0

What are you using for the file upload element? Is this in a form, webform etc.? With the Form API managed_file should do what you want.

2
  • Yes i am using a custom form with manage_file. This field is optional. Dec 10 '14 at 21:37
  • The managed_file element has processes attached to it by default. So that you do not override any necessary processes add new ones with $videoForm['profile_photos']['images']['field_photo']['#process'][] = 'new_process_function'; This might be the issue. Edit: you can run the new processes first with array_unshift() instead.
    – sareed
    Dec 10 '14 at 21:52
0

It's Working Example:

function yourModuleName_form_alter(&$form, &$form_state, $form_id) 
{       
   if($form_id == "test_node_form")
  {
    $form['field_file']['und'][0]['#process'][] = 'process_test_file';
  }
}


function process_test_file($element, &$form_state, $form)
{
   $element = file_managed_file_process($element, $form_state, $form);  
   $element['upload_button']['#submit'][] = 'validateCustomFile';
   return $element;
 }
function validateCustomFile($form,$form_state)
{   
  if($form_state['values']['field_file']['und'][0]['fid'] <= 0)
  {
    form_set_error('field_file', t('Please Select At Least One File.'));
  }
}

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.