使用hook_form_alter进行文件附件扩展的Drupal file_validate_extensions

时间:2011-11-16 22:33:10

标签: validation drupal drupal-hooks drupal-forms drupal-alter

我构建了一个模块,它使用名为form_mods_form_alter的函数来改变表单。 我想只允许jpg,jpeg,png文件类型。我所拥有的不是验证。它允许gif,我不想允许gif。

<?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
                                        )

                                )

                        )

                )

        )

1 个答案:

答案 0 :(得分:0)

上传验证程序需要继续使用该元素,而不是#ahah属性。试试这个:

$form['attachments']['wrapper']['new']['upload']['#upload_validators']['file_validate_extensions'][0] = 'png jpg jpeg';
相关问题