Hi All
I am writing my own module to create custom forms using the form api.
My forms have file upload fields and I would like to check if the user has entered a file name in the upload fields in the form validation function and not the submit function.
The #REQUIRED property won't work for us because the file upload field is required only when some other fields are filled.
The problem is in the validate function, $form_state['file1'], $form['file1']['#value'], _POST['file1']
and $file1 are all not available. No data returned even though user has entered a file name in the file1 field. In a normal php form, I would just need to test for $file1 to find out whether the user has entered anything in the file1 field.
Question: Is there anyway to check if the user has entered anything in the file upload field (i.e. '#type' = 'file') in the form validate function?
It is possible to use $file = file_save_upload in the form submit function to check if user has uploaded a file, but I would prefer to do it in the validate function.
It should be possible to check if user has entered anything in the file upload field in the Validated function because #REQUIRED is tested in the Validate function. (#REQUIRED won't work for me as mentioned above).
This issue may be similar to http://drupal.org/node/220944
Thank you very much in anticipation.
cmgui
function fxforms_3dprinterform() {
$form['#attributes'] = array(
'enctype' => 'multipart/form-data',
);
$form['file1'] = array(
'#type' => 'file',
'#size' => 24,
// '#required' => TRUE,
);
--
function fxforms_3dprinterform_validate($form, &$form_state) {
// all does not work
// $temp1 = $_POST['file1'];
// if ( $form['file1']['#value'] == '' ) {
// if ( trim($temp1) == '' ) {
// if ( trim($file1) == '' ) {
// if ($form_state['file1'] == '' ) {
// form_set_error('file1', t(' go to hell!'));
// }
}
function fxforms_3dprinterform_submit($form, &$form_state) {
$limits = array( 'extensions' => 'stl txt zip', 'file_size' => 100000000, 'user_size' => 0 );
$validators = array(
'file_validate_extensions' => array($limits['extensions']),
'file_validate_size' => array($limits['file_size'], $limits['user_size']),
);
// Save new file uploads.
if ( $file = file_save_upload('file1', $validators, file_directory_path())) {
$filedest = $file->timestamp . $user->name . $file->filename;
copy( $file->filepath, '/usr/local/www/drupal-6.6/sites/default/fxfiles/' . $filedest );
}
else
{
echo " nothing uploaded ";
exit;
}
Comments
Comment #1
cmgui commentedfound out that #REQUIRED is not allowed in file fields...
so maybe there is no way to check if user has entered something in a file field in the form validate function?
Comment #2
dipen chaudhary commentedcheck $form[#node] and you should be able to see your file field. print_r($form[#node]) then check the file field value. I've not tried this, but should work.
Comment #3
cmgui commentedthank you dipen
no luck.
print_r( $form['#node'] ) doesn't return anything.
$form_state['node'] also nothing.
when i do a var_dump($form) and var_dump($form_state), there is no 'node' in the array.
Comment #4
dpearcefl commentedDo you still need help with this issue?
Comment #5
kars-t commentedHi
I am closing this issue to clean up the issue queue. Feel free to reopen the issue if there is new information and the problem still resides. If not please make sure you close your issues that you don't need any more.
Maybe you can get support from the local user group. Please take a look at this list at groups.drupal.org.