FixFileField Plugin Demo

This demo shows how to create and use a mandatory file upload field in a Grav form.

To use the demo navigate to the Form page

Workaround

This plugin allows Grav form file fields to be mandatory by setting the attribute validate.required: true The workaround is needed because without it forms in which there is a mandatory file field can not be submitted at all.

Validation on file fields

This plugin validates on the presence and the number of uploaded files.

  • When validate.required is true the plugin upon Submit checks whether or not a file has been uploaded.
  • When the attribute limit is set, it checks whether or not the total number of files does not exceed that limit.
  • Likewise a new attribute lower_limit is introduced to set and validate on a minimum number of uploaded files.

Error messages

When validation on one or more of the above validation rules fails one or more error messages are shown like this:

file-field-fail-1

Language file

The error messages are defined in a languages.yaml file which is part of the plugin:

en:
  PLUGIN_FIXFILEFIELD:
    DEMO_TEXTS: 
      NAME_LABEL: 'Name'
      ATTACHMENT_LABEL: 'Attachment (1 PDF)'
      IMAGES_LABEL: 'Images (optional; minimum 2, maximum 3)'
    MESSAGES:
      UPLOAD_MIN: 'Upload at least %d files'
      UPLOAD_MAX: 'Upload no more than %d files'
      UPLOAD_MIN_MAX: 'Upload at least %d but no more than %d files'
    VALIDATION:
      ERRORS:
        REQUIRED: 'This field is mandatory!'
        UPLOAD_MIN:
          SINGULAR: 'Please upload at least one file'
          PLURAL: 'Please upload at least %d files'
        UPLOAD_MAX:
          SINGULAR: 'Please upload just one file'
          PLURAL: 'Please upload no more than %d files'

de:
  PLUGIN_FIXFILEFIELD:
    DEMO_TEXTS:
      NAME_LABEL: 'Name'
      ATTACHMENT_LABEL: 'Anhang (1 PDF)'
      IMAGES_LABEL: 'Bilder (optional; mindestens 2, maximal 3)'
    MESSAGES:
      UPLOAD_MIN: 'Laden Sie mindestens %d Dateien hoch'
      UPLOAD_MAX: 'Laden Sie maximal %d Dateien hoch'
      UPLOAD_MIN_MAX: 'Laden Sie mindestens %d, aber nicht mehr als %d Dateien hoch'
    VALIDATION:
      ERRORS:
        REQUIRED: 'Dieses Feld ist obligatorisch!'
        UPLOAD_MIN:
          SINGULAR: 'Bitte laden Sie mindestens eine Datei hoch'
          PLURAL: 'Bitte laden Sie mindestens %d Dateien hoch'
        UPLOAD_MAX:
          SINGULAR: 'Bitte laden Sie nur eine Datei hoch'
          PLURAL: 'Bitte laden Sie maximal %d Dateien hoch'