By timpiche on
I am developing a custom module and I understand the form API to a degree. I am having trouble with the upload function. I need an upload box to upload a file into a child directory in the files folder. It also needs to validate that the file is a pdf file. I have posted my code below. Hopefully someone can help!
function schematics_perm() {
return array('access schematics');
}
function schematics_menu() {
$items = array();
$items['schematics/form'] = array(
'title' => t('Insert Schematics into the database'),
'page callback' => 'schematics_form',
'access arguments' => array('access schematics'),
'description' => t('My form'),
'type' => MENU_CALLBACK,
);
return $items;
}
function schematics_form() {
return drupal_get_form('schematics_my_form');
}
function schematics_my_form($form_state) {
$form['insert'] = array(
'#type' => 'fieldset',
'#title' => t('Insert Schematic Into Database'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['insert']['id'] = array(
'#type' => 'hidden',
);
$form['insert']['model'] = array(
'#type' => 'textfield',
'#title' => t('Model Number'),
'#description' => "Please enter the model number of the machine i.e. TTB4552",
'#size' => 60,
'#maxlength' => 60,
);
$form['insert']['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#description' => "Please enter the description of the schematic i.e. Brush Base Assembly or Vac pod assembly",
'#size' => 60,
'#maxlength' => 60,
);
$form['insert']['min'] = array(
'#type' => 'textfield',
'#title' => t('Serial Number Start'),
'#description' => "Please enter the first 4 digits of the starting serial number",
'#size' => 60,
'#maxlength' => 4,
);
$form['insert']['max'] = array(
'#type' => 'textfield',
'#title' => t('Serial Number End'),
'#description' => "Please enter the first 4 digits of the ending serial number",
'#size' => 60,
'#maxlength' => 4,
);
$form['insert']['schematic'] = array(
'#type' => 'textfield',
'#title' => t('Schematic File Name'),
'#description' => "Please enter the file name of the schematic. This file must be uploaded to the site via FTP in the sites/default/files/schematics folder.",
'#size' => 60,
'#maxlength' => 20,
);
$form['insert']['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
$form['insert']['clear'] = array(
'#type' => 'submit',
'#value' => 'Reset form',
'#validate' => array('schematics_my_form_clear'),
);
return $form;
}
function schematics_my_form_clear($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
function schematics_my_form_validate($form, &$form_state) {
$model = $form_state['values']['model'];
$min = $form_state['values']['min'];
$max = $form_state['values']['max'];
$schematic = $form_state['values']['schematic'];
if (!$model) {
form_set_error('model', 'Please enter a model number');
}
if (!$min) {
form_set_error('min', 'Please enter a starting serial number.');
}
if (!$max) {
form_set_error('max', 'Please Enter a starting serial number.');
}
if (!$schematic) {
form_set_error('schematic', 'Please Enter the file name including the file extension i.e. <strong>schematic.pdf</strong>.');
}
}
function schematics_my_form_submit($form, &$form_state) {
db_query("INSERT INTO schematics (id, model, description, min, max, schematic) VALUES ('%s', '%s', '%s', '%s', '%s', '%s')", $form_state['hidden']['id'], $form_state['values']['model'], $form_state['values']['description'], $form_state['values']['min'], $form_state['values']['max'], $form_state['values']['schematic']);
drupal_set_message(t('The form has been submitted.'));
}