marc
marc

Reputation: 11

Typo3 Form Validation - Allowed File Types and Maximum File Size

Is it possible to validate a typo3 (vers. 7.6.23) form for allowed file types (in my example only *.pdf) and maximum file size (in my example maximum 5mb)? Maybe with the rules function?

My form looks like this:

enctype = multipart/form-data
method = post
prefix = tx_form
confirmation = 
postProcessor {
    1 = mail
    1 {
        recipientEmail = [email protected]
        senderEmail = [email protected]
        subject = email subject
    }
}
...  
40 = FIELDSET
40 {
    20 = FILEUPLOAD
    20 {
        type = file
        name = Anschreiben
        label {
            value = Anschreiben
        }
    }
}
...

rules {
    1 = required
    1 {
        showMessage = 
        message = Benötigt
        error = Dies ist ein Pflichtfeld
        element = Vorname
    }
    2 = required
    2 {
        showMessage = 
        message = Benötigt
        error = Dies ist ein Pflichtfeld
        element = Nachname
    }
    3 = email
    3 {
        showMessage = 
        message = ([email protected])
        error = Dies ist keine gültige E-Mail-Adresse
        element = E-Mail
    }
}

UPDATE

Thank you for pointing me towards FileAllowedTypesValidator and FileMaximumSizeValidator.

I tried adding the idea to the rules:

rules {

    4 = fileallowedtypes 
        4 { 
                breakOnError = 0 
                showMessage = 
                message = (%allowedTypes) 
                error = only pdf please!
                types = application/pdf
                element = Anschreiben 
        } 
    5 = filemaximumsize 
        5 { 
                breakOnError = 0 
                showMessage = 
                message = The file has to be smaller as %maximum 
                error = file is too big!
                maximum = 5242880 
                element = Anschreiben 
        } 

}

Since I have more than one FileUpload-Field i added those two rules for every field. The query now works and I get an error when I upload the wrong file type or a file bigger than 5 MB.

Only problem is, that i cant leave the FileUpload-Fields empty; i always get the "wrong filetype" error.

Upvotes: 1

Views: 1044

Answers (1)

Pravin Vavadiya
Pravin Vavadiya

Reputation: 3207

You can use below typoscript.

plugin.tx_form {
    settings {
       registeredValidators {
        fileallowedtypes {
            displayName = Allowed mimetypes for file
            className = TYPO3\CMS\Form\Domain\Validator\FileAllowedTypesValidator
        }

        filemaximumsize {
            displayName = Maximum size for file (bytes)
            className = TYPO3\CMS\Form\Domain\Validator\FileMaximumSizeValidator
        }
       }
    }
}

Upvotes: 1

Related Questions