Skip to content

Commit f371a91

Browse files
committed
Updated validation
1 parent 99f2b32 commit f371a91

3 files changed

Lines changed: 82 additions & 13 deletions

File tree

docs/01-basic-configuration.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
## Basic configuration
22

3-
The full default configuration looks like this and this is a common configuration, independent if you are using a file
4-
system or using a cloud provider.
3+
The minimal configuration:
54

65
```
7-
protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface
8-
{
9-
// you must define the `photo`'s column file time
10-
$schema->setColumnType('photo', 'upload.file');
11-
12-
// ...
13-
14-
return parent::_initializeSchema($schema);
15-
}
6+
$this->addBehavior(\FileUploader\Model\Behavior\UploadBehavior::class, ['photo']);
167
```
178

18-
then add behavior:
9+
The minimal configuration equal with the basic configuration:
1910

2011
```
2112
$this->addBehavior(\FileUploader\Model\Behavior\UploadBehavior::class, [
@@ -30,6 +21,8 @@ $this->addBehavior(\FileUploader\Model\Behavior\UploadBehavior::class, [
3021
]);
3122
```
3223

24+
This minimal configuration uses the file system, if you want to use a cloud provider you can extend this config.
25+
3326
***filePathProcessor***
3427

3528
Default value: null

docs/05-validation.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ This plugin offers basic validation.
44

55
You can validate your file against:
66

7+
* existence
78
* mime type
89
* extension
910
* file size
@@ -14,6 +15,7 @@ Example:
1415
$this->addBehavior(\FileUploader\Model\Behavior\UploadBehavior::class, ['image' => [
1516
// ... your other configs here ....
1617
'validation' => [
18+
'allowEmptyFile' => true,
1719
'allowedExtensions' => ['jpg', 'pdf'],
1820
'allowedMimeTypes' => ['application/pdf', 'image/jpg'],
1921
'fileSize' => [
@@ -24,6 +26,12 @@ $this->addBehavior(\FileUploader\Model\Behavior\UploadBehavior::class, ['image'
2426
]]);
2527
```
2628

29+
***validation.allowEmptyFile***
30+
31+
Default value: true
32+
33+
The file is required or optional. Possible values: true, false, 'created', 'updated'
34+
2735
***validation.allowedExtensions***
2836

2937
Default value: []
@@ -61,6 +69,7 @@ $this->addBehavior(\FileUploader\Model\Behavior\UploadBehavior::class, ['image'
6169
'table' => \App\Model\Table\MyUploadedFilesTable::class,
6270
'validation' => [
6371
'method' => 'image',
72+
'allowEmptyFile' => true,
6473
'allowedExtensions' => ['jpg', 'pdf'],
6574
'allowedMimeTypes' => ['application/pdf', 'image/jpg'],
6675
'fileSize' => [

src/Model/Behavior/UploadBehavior.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Cake\ORM\Behavior;
1313
use Cake\ORM\TableRegistry;
1414
use Cake\Utility\Hash;
15+
use Cake\Utility\Text;
16+
use Cake\Validation\Validation;
1517
use FileUploader\FilePathProcessor\CloudProcessor;
1618
use FileUploader\FilePathProcessor\DefaultProcessor;
1719
use FileUploader\Model\Table\UploadedFilesTable;
@@ -72,11 +74,76 @@ public function initialize(array $config): void
7274
$this->_config = $configs;
7375

7476
$schema = $this->table()->getSchema();
77+
$validator = $this->table()->getValidator();
78+
7579
/** @var string $field */
7680
foreach (array_keys($this->getConfig()) as $field) {
7781
$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+
}
78146
}
79-
$this->table()->setSchema($schema);
80147
}
81148

82149
/**

0 commit comments

Comments
 (0)