neos / form-builder

Flow Form Framework integration into Neos CMS
GNU General Public License v3.0
18 stars 31 forks source link

Add Validator for FileUpload #138

Open Schuppel1 opened 2 months ago

Schuppel1 commented 2 months ago

In this PR the FileUpload FormElement getting a validators

form-builder/Configuration/NodeTypes.FormElements.FileUpload.yaml

    'allowedExtensions':
      type: array
      ui:
        label: i18n
        reloadIfChanged: true
        inspector:
          group: 'formElement'
          editor: 'Neos.Neos/Inspector/Editors/SelectBoxEditor'
          editorOptions:
            values:
              'pdf':
                label: '.pdf'
                icon: 'icon-file-pdf-o'
              'xls':
                label: '.xls'
                icon: 'icon-file-excel-o'
              'xlsx':
                label: '.xlsx'
                icon: 'icon-file-excel-o'
              'doc':
                label: '.doc'
                icon: 'icon-file-text'
              'docx':
                label: '.docx'
                icon: 'icon-file-text'
              'odt':
                label: '.odt'
                icon: 'icon-file-text'
              'csv':
                label: '.csv'
                icon: 'icon-file-text'

The code above is removed from FileUpload and become an validator Configuration/NodeTypes.Validator.FileExtension.yaml

'Neos.Form.Builder:FileExtensionValidator':
  superTypes:
    'Neos.Form.Builder:AbstractValidator': true
  ui:
    label: 'File-Extension Validator'
    icon: 'icon-file-archive'
    inspector:
      groups:
        'validator':
          icon: 'icon-filter'
  properties:
    'allowedExtensions':
      type: array
      ui:
        label: i18n
        reloadIfChanged: true
        inspector:
          group: 'validator'
          editor: 'Neos.Neos/Inspector/Editors/SelectBoxEditor'
          editorOptions:
            values:
              'pdf':
                label: '.pdf'
                icon: 'icon-file-pdf-o'
              'xls':
                label: '.xls'
                icon: 'icon-file-excel-o'
              'xlsx':
                label: '.xlsx'
                icon: 'icon-file-excel-o'
              'doc':
                label: '.doc'
                icon: 'icon-file-text'
              'docx':
                label: '.docx'
                icon: 'icon-file-text'
              'odt':
                label: '.odt'
                icon: 'icon-file-text'
              'csv':
                label: '.csv'
                icon: 'icon-file-text'
              'zip':
                label: '.zip'
                icon: 'icon-file-text'

Another Validator is the FileSizeValidator

'Neos.Form.Builder:FileSizeValidator':
  superTypes:
    'Neos.Form.Builder:AbstractValidator': true
  ui:
    label: 'File-Size range Validator'
    icon: 'icon-arrows-alt-h'
    inspector:
      groups:
        'validator':
          icon: 'icon-sort-numeric-asc'
  properties:
    'minimum':
      type: integer
      defaultValue: 0
      ui:
        label: i18n
        inspector:
          group: 'validator'
    'maximum':
      type: integer
      defaultValue: 100
      ui:
        label: i18n
        inspector:
          group: 'validator'

A Mixin: FileValidatorsMixin

'Neos.Form.Builder:FileValidatorsMixin':
  abstract: true
  superTypes:
    'Neos.Form.Builder:ValidatorsMixin': true
  childNodes:
    'validators':
      constraints:
        nodeTypes:
          'Neos.Form.Builder:FileSizeValidator': true
          'Neos.Form.Builder:FileExtensionValidator': true

Now inside of NEOS it looks like this: image

Important:

Dependencies with another Package! Neos:Form Neos.Form/Configuration/Settings.yaml

It is needed to add the following:

          'Neos.Flow:FileSize':
            implementationClassName: Neos\Flow\Validation\Validator\FileSizeValidator
          'Neos.Flow:FileExtension':
            implementationClassName: Neos\Flow\Validation\Validator\FileExtensionValidator

to:

Neos:
  Form:
    presets:
      default:
        validatorPresets:

Neos:Form Neos.Form/Classes/FormElements/FileUpload.php Remove old FileExtensionValidation Code

        $fileTypeValidator = new FileTypeValidator(array('allowedExtensions' => $this->properties['allowedExtensions']));
        $this->addValidator($fileTypeValidator);

This can be removed.

Problem:

We need a migration: for smooth use. I'm not good with migrations.