I have a Drupal 7 custom form where the image or file field is not required. When the form is submitted and no file/image has been uploaded, I get the following 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!
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!");
}
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.