2011-11-16 38 views
0

我使用名爲form_mods_form_alter的函數構建了一個改變窗體的模塊。 我想只允許jpg,jpeg,png文件類型。我所擁有的不是驗證。這是允許gif,我不想讓gif。使用hook_form_alter的文件附件擴展Drupal file_validate_extensions

<?php 
function form_mods_form_alter($form, $form_state, $form_id) { 
    // Ad form 
    if($form_id == 'ad_node_form'){ 
     $form['attachments']['wrapper']['new']['upload']['#description'] = 'BBGI test The maximum upload size is 1 MB. Only files with the following extensions may be uploaded: jpg jpeg png.'; 
     $form['attachments']['wrapper']['new']['upload']['#ahah']['#upload_validators']['file_validate_extensions'][0] = 'png jpg jpeg'; 
    } 
} 
?> 

這是我用print_r($ form)得到的;

[attachments] => Array 
     (
      [#type] => fieldset 
      [#access] => 1 
      [#title] => File attachments 
      [#collapsible] => 1 
      [#collapsed] => 1 
      [#description] => Changes made to the attachments are not permanent until you save this post. The first "listed" file will be included in RSS feeds. 
      [#prefix] => 

      [#suffix] => 

      [#weight] => 30 
      [wrapper] => Array 
       (
        [#prefix] => 

        [#suffix] => 

        [#theme] => upload_form_new 
        [#cache] => 1 
        [new] => Array 
         (
          [#weight] => 10 
          [upload] => Array 
           (
            [#type] => file 
            [#title] => Attach new file 
            [#size] => 40 
            [#description] => BBGI test The maximum upload size is 1 MB. Only files with the following extensions may be uploaded: jpg jpeg png. 
            [#ahah] => Array 
             (
              [#upload_validators] => Array 
               (
                [file_validate_extensions] => Array 
                 (
                  [0] => png jpg jpeg 
                 ) 

               ) 

             ) 

           ) 

          [attach] => Array 
           (
            [#type] => submit 
            [#value] => Attach 
            [#name] => attach 
            [#ahah] => Array 
             (
              [path] => upload/js 
              [wrapper] => attach-wrapper 
              [progress] => Array 
               (
                [type] => bar 
                [message] => Please wait... 
               ) 

              [#upload_validators] => Array 
               (
                [file_validate_extensions] => Array 
                 (
                  [0] => png jpg jpeg 
                 ) 

               ) 

             ) 

            [#submit] => Array 
             (
              [0] => node_form_submit_build_node 
             ) 

           ) 

         ) 

       ) 

     ) 
+0

這裏的一個常見缺陷是隻測試從管理(用戶1)帳戶,爲[file_validate_extensions](http://api.drupal.org/api/drupal/includes--file。 inc/function/file_validate_extensions/6)跳過該用戶的驗證。 –

回答

0

上傳驗證器需要在元素上,而不是在#ahah屬性上。嘗試:

$form['attachments']['wrapper']['new']['upload']['#upload_validators']['file_validate_extensions'][0] = 'png jpg jpeg'; 
+0

我試過了,它仍然允許gif上傳並列出它。 – EricP