validationploneploneformgenplone-5.x

How to create a PloneFormGen validator for file types?


I'm creating a Plone form, using PloneFormGen, and I need a PDF file upload field. How can I write a validator that will test if the uploaded file is a PDF file, or if at least has a .pdf extension?


Solution

  • You can add

    python: test('.pdf' in value.filename, False, 'File must be a PDF')
    

    To the PloneFormGen validator for each file upload item. This is a pretty simple check for .pdf. It's not ideal but does a simple string check. A better option would be to check the mime-type of the file on the server.