1

I have a (Drupal 7) custom form where the image or file field is not required, however when the form is submitted and the image or file field is NULL I get the below errors

Notice: Undefined index: storage in jobform_my_form_submit() (line 136 of /var/www/html/drupaltst/sites/all/modules/jobpackage/jobform.module).

Warning: Creating default object from empty value in jobform_my_form_submit() (line 141 of var/www/html/drupaltst/sites/all/modules/jobpackage/jobform.module).

Notice: Undefined property: stdClass::$uri in file_save() (line 601 of /var/www/html/drupaltst/includes/file.inc).

Any help is appreciated!

<?php
/**
  * Implements hook_permission().
  */
function jobform_permission() {
  return array(
    'add jobs module' => array(
       'title' => t('Administer permission for your module'),
       'description' => t('Some description that would appear on the permission page..'),
    ),
  );
}

function jobform_menu() {
  $items = array();
  //$items['jobform/form'] = array(
  $items['jobform'] = array(
    'title' => t('Job Form Details'),
    'page callback' => 'jobform_form',
    'page arguments' => array('jobform_form'),
    'access arguments' => array('add jobs module'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function jobform_form() {
  return drupal_get_form('jobform_my_form');
}

function jobform_my_form($form, &$form_state) {
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#maxlength' => 100,
    '#prefix' => '<div class="titletle"><img src="/drupaltst/sites/default/files/images/title_icon.png" height="50" width="45">', 
     '#suffix' => '</img></div>',
     '#attributes' => array(
       'placeholder' => t('Enter a Short Title'),
     ), 
   );    
   $form['descr'] = array(
     '#type' => 'textarea',
     '#title' => t('Description'),
     '#required' => TRUE,
     '#size' => 150,
     '#maxlength' => 5000,
     '#prefix' => '<div class="titledescr"><img src="/drupaltst/sites/default/files/images/descr_req_icon.png" height="50" width="45">', 
     '#suffix' => '</img></div>',
     '#attributes' => array(
       'placeholder' => t('Enter some specific details about the service you provide!'),
     ), 
   );       
   $form['location'] = array(
     '#type' => 'textfield',
     '#title' => t('Enter Your Location'),
     '#required' => TRUE,
     '#size' => 75,
     '#prefix' => '<div class="titleloc"><img src="/drupaltst/sites/default/files/images/loc_icon.png" height="50" width="41">', 
     '#suffix' => '</img></div>',
     '#attributes' => array(
      'placeholder' => t('Where Can the Work Be Completed? (i.e. Online, City, State, Zip)'),
     ),    
   );

   //Image Field
   $form['image'] = array(
     '#type' => 'file',
     '#title' => t('Upload a Photo'),
     '#description' => t('Allowed extensions: gif png jpg jpeg'),
     '#prefix' => '<br><div class="titlefile"><img src="/drupaltst/sites/default/files/images/upload_image_icon.png" height="50" width="41">', 
     '#suffix' => '</img></div>',
   );
   $form['submit'] = array(
     '#type' => 'submit',
     '#value' => 'Submit',
     '#prefix' => '<br><div class="titlesubmit">', 
     '#suffix' => '</div>',
   );

   return $form;
 }

 function jobform_my_form_validate($form, &$form_state) {
   $file = file_save_upload('image', array(
     // Validates file is really an image.
     'file_validate_is_image' => array(),
     // Validate extensions.
     'file_validate_extensions' => array('png gif jpg jpeg'),
   ));
   // If the file passed validation:
   if ($file) {
     // Move the file into the Drupal file system.
     if ($file = file_move($file, 'public://images/')) {
       // Save the file for use in the submit handler.
       //$form_state['image'] = $file;
       $form_state['storage']['image'] = $file;
     }
     else {
       form_set_error('image', t("Failed to write the uploaded file to the site's file folder."));
     }
   } 
   else {
     form_set_error('image', t('No file was uploaded.'));
     //$file = file_move($file, 'public://images/title_icon.png');
   }
 }

 function jobform_my_form_submit($form, $form_state) {    
   $file = $form_state['storage']['image'];
   // We are done with the file, remove it from storage.
   //unset($form_state['image']);
   unset($form_state['storage']['image']);
   // Make the storage of the file permanent.
   $file->status = FILE_STATUS_PERMANENT;
   // Save file status.
   file_save($file);
   global $user; 
   $na = 0;
   $istat = 1;
   db_insert('job_mp_catalog')
     ->fields(array(
       'user_id' => $user->uid,
       'title' => $form_state['values']['title'],
       'descr' => $form_state['values']['descr'],
       'location' => $form_state['values']['location'],
       'image' => $file->fid,))
     ->execute();     
   drupal_set_message("successfully saved ");
   //Example:
   //drupal_goto("user/".$user->uid."/edit");
   drupal_goto("content/test-page");
   //drupal_set_message("successfully saved Job!");
 }
  • you can probably use dpm() on $form_state above $file = $form_state['storage']['image']; to check if storage index exist or not. And then use checks to avoid exceptions. Also there were extra curly braces. – Vishal Kumar Apr 12 '16 at 5:54
0

Just try to validate on submit function as follow

function my_module_image_form_submit($form, &$form_state) {
  // Upload file to upload_directory in the public files dir.
  $file = file_save_upload('image', array(
    'file_validate_is_image' => array(),
    'file_validate_image_resolution' => array('500x500', '100x100'),
  ), 'public://uploaded_images/', FILE_EXISTS_RENAME);

  // Check if image upload was success.
  if ($file) {
    drupal_set_message(t('Image successfully uploaded.'), 'status');
  }
  else {
    form_set_error('image', t('Image was not uploaded.'));
  }
}

For more info please see this article.

  • That certainly helped, Thanks! I am getting this error "Notice: Trying to get property of non-object in jobform_my_form_submit()" at 'image' => $file->fid,)) when I do not upload an image. – Keith Apr 12 '16 at 15:50
0

Figured out the 'image' => $file->fid, part in the db_insert. I used visabhishek suggestion above but for the db_insert, I did the below with removing "'image' => $file->fid," in the else section.

Thanks for the help!

if ($file) {
   db_insert('job_mp_catalog')
     ->fields(array(
       'user_id' => $user->uid,
       'title' => $form_state['values']['title'],
       'descr' => $form_state['values']['descr'],
       'location' => $form_state['values']['location'],
       'image' => $file->fid,))
     ->execute();  

}
  else {  
      db_insert('job_mp_catalog')
        ->fields(array(
       'user_id' => $user->uid,
       'title' => $form_state['values']['title'],
       'descr' => $form_state['values']['descr'],
       'location' => $form_state['values']['location'],
     ->execute();
}   

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.