0

我使用名为 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
                                        )

                                )

                        )

                )

        )
4

1 回答 1

0

上传验证器需要在元素上进行,而不是在#ahah属性上。尝试这个:

$form['attachments']['wrapper']['new']['upload']['#upload_validators']['file_validate_extensions'][0] = 'png jpg jpeg';
于 2011-11-16T22:45:01.407 回答