【问题标题】:In Drupal, how to validate the contents of a file uploaded using webforms and *not* upload if it fails validation?在 Drupal 中,如何验证使用 webforms 上传的文件的内容,如果验证失败,如何*不*上传?
【发布时间】:2016-11-30 21:22:17
【问题描述】:

我正在实施一个以特定方式接受具有目录结构的档案的网站。我想在接受之前检查 zipfile 中的目录结构。我尝试了以下方法(请参阅 cmets inline):

<?php

使用网络表单验证:

// 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();
    }
}

使用表单 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;
}

谢谢!

【问题讨论】:

    标签: validation webforms drupal-7


    【解决方案1】:

    它的工作示例:

    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;
    }
    

    【讨论】:

      【解决方案2】:

      我认为你在完成任务时错过了一点。您只是忘记将错误发回。

      在 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)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-22
        • 2012-10-23
        • 1970-01-01
        • 2014-03-31
        • 2022-11-22
        • 1970-01-01
        • 2019-09-20
        相关资源
        最近更新 更多