|
12 | 12 | use Cake\ORM\Behavior; |
13 | 13 | use Cake\ORM\TableRegistry; |
14 | 14 | use Cake\Utility\Hash; |
| 15 | +use Cake\Utility\Text; |
| 16 | +use Cake\Validation\Validation; |
15 | 17 | use FileUploader\FilePathProcessor\CloudProcessor; |
16 | 18 | use FileUploader\FilePathProcessor\DefaultProcessor; |
17 | 19 | use FileUploader\Model\Table\UploadedFilesTable; |
@@ -72,11 +74,76 @@ public function initialize(array $config): void |
72 | 74 | $this->_config = $configs; |
73 | 75 |
|
74 | 76 | $schema = $this->table()->getSchema(); |
| 77 | + $validator = $this->table()->getValidator(); |
| 78 | + |
75 | 79 | /** @var string $field */ |
76 | 80 | foreach (array_keys($this->getConfig()) as $field) { |
77 | 81 | $schema->setColumnType($field, 'upload.file'); |
| 82 | + $this->table()->setSchema($schema); |
| 83 | + |
| 84 | + // set validation rules |
| 85 | + $validator->allowEmptyFile( |
| 86 | + $field, |
| 87 | + __d('file_uploader', 'This field is required'), |
| 88 | + $this->getConfig($field . '.validation.allowEmptyFile', true) |
| 89 | + ); |
| 90 | + |
| 91 | + $allowedExtensions = $this->getConfig($field . '.validation.allowedExtensions'); |
| 92 | + if (is_array($allowedExtensions) && !empty($allowedExtensions)) { |
| 93 | + $validator->add( |
| 94 | + $field, |
| 95 | + 'extension', |
| 96 | + [ |
| 97 | + 'rule' => ['extension', $allowedExtensions], |
| 98 | + 'message' => __d( |
| 99 | + 'file_uploader', |
| 100 | + 'Invalid file extension. Allowed file types are: {0}', |
| 101 | + implode(', ', $allowedExtensions) |
| 102 | + ) |
| 103 | + ] |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + $allowedMimeTypes = $this->getConfig($field . '.validation.allowedMimeTypes'); |
| 108 | + if (is_array($allowedExtensions) && !empty($allowedMimeTypes)) { |
| 109 | + $validator->add( |
| 110 | + $field, |
| 111 | + 'mimeType', |
| 112 | + [ |
| 113 | + 'rule' => ['mimeType', $allowedMimeTypes], |
| 114 | + 'message' => __d( |
| 115 | + 'file_uploader', |
| 116 | + 'Invalid file type. Allowed file types are: {0}', |
| 117 | + implode(', ', $allowedMimeTypes) |
| 118 | + ) |
| 119 | + ] |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + $minSize = $this->getConfig($field . '.validation.fileSize.min'); |
| 124 | + if (is_string($minSize)) { |
| 125 | + $validator->add( |
| 126 | + $field, |
| 127 | + 'fizeSizeMin', |
| 128 | + [ |
| 129 | + 'rule' => ['fileSize', Validation::COMPARE_GREATER_OR_EQUAL, Text::parseFileSize($minSize)], |
| 130 | + 'message' => __d('file_uploader', 'The file must be at least {0}', $minSize) |
| 131 | + ] |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + $maxSize = $this->getConfig($field . '.validation.fileSize.max'); |
| 136 | + if (is_string($maxSize)) { |
| 137 | + $validator->add( |
| 138 | + $field, |
| 139 | + 'fizeSizeMax', |
| 140 | + [ |
| 141 | + 'rule' => ['fileSize', Validation::COMPARE_LESS_OR_EQUAL, Text::parseFileSize($maxSize)], |
| 142 | + 'message' => __d('file_uploader', 'The file must not exceed {0}', $maxSize) |
| 143 | + ] |
| 144 | + ); |
| 145 | + } |
78 | 146 | } |
79 | | - $this->table()->setSchema($schema); |
80 | 147 | } |
81 | 148 |
|
82 | 149 | /** |
|
0 commit comments