在Drupal中,如果验证无法验证,如何验证使用webforms上传的文件的内容和* not * upload?

时间:2016-07-27 03:31:05

标签: validation webforms drupal-7

我正在实现一个以特定方式接受具有目录结构的存档的站点。我想在接受之前检查zipfile中的目录结构。我尝试了以下内容(请参阅内联评论):

<?php

使用Webform验证:

// using the webform validation module and its hooks 
function testsuite_ziptest_webform_validation_validators()
{
    return array(
        "validate_zip_file"=> array(
            'name' => "testsuite: Validate Zipfile" ,
            'component_types' => array(
                'select',
                'file',
            ),
            'description' => t('Verifies that the contents of the zipfile adhere to the specifications of testsuite.'),
        )
    );
}

function testsuite_ziptest_webform_validation_validate($validator_name, $items, $components, $rule)
{
    $errors = array();

    if($items)
    {
        switch($validator_name)
        {
        case "validate_zip_file":    
            drupal_set_message(t('Validate function called'));
            foreach($items as $key=>$value)
            {
                drupal_set_message($key);
                $v = _webform_validation_flatten_array($value);
                drupal_set_message($v);
            }

            // tried to get the $fid and access the file using the fid.
            // item_6 is the key of the file field that I selected while
            // enabling webform validation.
            // This fails saying no such file exists when the ziparchive 
            // object tries to open it.

            $fid = $items['item_6'];

            if(!empty($fid))
            {
                $za = new ZipArchive();
                $file = file_load($fid);
                $za->open($file->uri);
                for($i = 0; $i < $za->numFiles; $i++)
                {
                    $stat = $za->statIndex($i);
                    drupal_set_message($stat['name']);
                }
                $za->close();
            }
            break;
        }
    }

    return $errors;
}

使用hook_file_validate

// this works, but there might be other files that may
// be uploaded to the site and I only want it to trigger
// when the file is uploaded as a part of a webform, not
// for all file uploads.
function testsuite_ziptest_file_validate($file)
{
    if(!empty($file->filename))
    {
        $za = new ZipArchive();
        $za->open($file->uri);
        for($i = 0; $i < $za->numFiles; $i++)
        {
            $stat = $za->statIndex($i);
            drupal_set_message($stat['name']);
        }
        $za->close();
    }
}

使用Forms API(?)

// The following two methods that uses the form api on the webform
// has the same issue as the webform validation module. I can't get
// any reference to the file.
// There is a reference through a "completed form" key but I don't know
// if this is best practice 
// die statements were used for debugging 
function testsuite_ziptest_form_alter(&$form, &$form_state, $form_id)
{
    if($form_id == 'webform_client_form_1')
    {
        array_unshift($form['#validate'], 'testsuite_ziptest_form_validate');
        return $form;
    }
}

function testsuite_ziptest_form_validate($form, &$form_state)
{
    echo '<pre>'; print_r($form_state); echo '</pre>';
    die();
    $fid = $form_state['values']['submitted']['attachment'];
    if(!empty($fid))
    {
        $za = new ZipArchive();
        $file = file_load($fid);
        $za->open($file->uri);
        for($i = 0; $i < $za->numFiles; $i++)
        {
            $stat = $za->statIndex($i);
            drupal_set_message($stat['name']);
        }
        $za->close();
    }
    else
    {

    }
    die();
    return;
}

谢谢!

2 个答案:

答案 0 :(得分:0)

我觉得你在完成你的功能时错过了一点。你只是忘了发回错误。

在Webforms验证过程中,如果出现问题,您必须在$ errors数组中发回一些元素。

$errors[$key] = t('%item is not good', array('%item' => $components[$key]['name']));

Here is an example使用此方法。

在Drupal表单验证过程中,如果出现问题,则必须使用form_set_error,并使用组合名称和错误消息。验证然后自动停止,表单将不会提交......

在hook_file_validate方法中,您还必须发回一系列错误,验证器将使用它来停止提交(使用form_set_error)。

答案 1 :(得分:0)

其工作范例:

function yourmoduleNma_file_validate($file,$validators)
{   
   $errors = array();
   $filename = $file->filename;
   $isValid_Extention_Size = explode('.',$filename);            
   if(count($isValid_Extention_Size) > 2){          
     $errors[] = 'Extention is not valid of this file';
    }
    elseif($file->filesize <= 0)
    {           
       $errors[] = 'File size should be greater than 0 Bytes and less than 5 MB';
   }
   return $errors;
}