-
Notifications
You must be signed in to change notification settings - Fork 254
Upload validation #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upload validation #374
Changes from all commits
fef2417
f140ff7
c9271e4
d563e22
264fe47
293c0db
ca927c3
8287d64
e9cbbae
cb91a6f
4911745
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
Validation | ||
---------- | ||
|
||
By default, no validation rules are loaded or attached to the table. You must | ||
explicitly load the validation provider(s) and attach each rule if needed. | ||
|
||
Installation | ||
^^^^^^^^^^^^ | ||
|
||
This plugin allows you to only load the validation rules that cover you needs. | ||
At this point there are 3 validation providers: | ||
|
||
- UploadValidation (validation rules useful for any upload) | ||
- ImageValidation (validation rules specifically for images) | ||
- DefaultValidation (loads all of the above) | ||
|
||
Since by default, no validation rules are loaded, you should start with that: | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->provider('upload', \Josegonzalez\Upload\Validation\UploadValidation::class); | ||
// OR | ||
$validator->provider('upload', \Josegonzalez\Upload\Validation\ImageValidation::class); | ||
// OR | ||
$validator->provider('upload', \Josegonzalez\Upload\Validation\DefaultValidation::class); | ||
|
||
?> | ||
|
||
Afterwards, you can use its rules like: | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'customName', [ | ||
'rule' => 'nameOfTheRule', | ||
'message' => 'yourErrorMessage', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
It might come in handy to only use a validation rule when there actually is an uploaded file: | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'customName', [ | ||
'rule' => 'nameOfTheRule', | ||
'message' => 'yourErrorMessage', | ||
'provider' => 'upload', | ||
'on' => function($context) { | ||
return isset($context['data']['file']) && $context['data']['file']['error'] == UPLOAD_ERR_OK; | ||
} | ||
]); | ||
|
||
?> | ||
|
||
More information on conditional validation can be found `here <http://book.cakephp.org/3.0/en/core-libraries/validation.html#conditional-validation>`__. | ||
|
||
UploadValidation | ||
^^^^^^^^^^^^^^^^ | ||
|
||
**isUnderPhpSizeLimit** | ||
|
||
Check that the file does not exceed the max file size specified by PHP | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add an example for each one? |
||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileUnderPhpSizeLimit', [ | ||
'rule' => 'isUnderPhpSizeLimit', | ||
'message' => 'This file is too large', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isUnderFormSizeLimit** | ||
|
||
Check that the file does not exceed the max file size specified in the | ||
HTML Form | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileUnderFormSizeLimit', [ | ||
'rule' => 'isUnderFormSizeLimit', | ||
'message' => 'This file is too large', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isCompletedUpload** | ||
|
||
Check that the file was completely uploaded | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileCompletedUpload', [ | ||
'rule' => 'isCompletedUpload', | ||
'message' => 'This file could not be uploaded completely', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isFileUpload** | ||
|
||
Check that a file was uploaded | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileFileUpload', [ | ||
'rule' => 'isFileUpload', | ||
'message' => 'There was no file found to upload', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isSuccessfulWrite** | ||
|
||
Check that the file was successfully written to the server | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileSuccessfulWrite', [ | ||
'rule' => 'isSuccessfulWrite', | ||
'message' => 'This upload failed', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isBelowMaxSize** | ||
|
||
Check that the file is below the maximum file upload size (checked in | ||
bytes) | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileBelowMaxSize', [ | ||
'rule' => ['isBelowMaxSize', 1024], | ||
'message' => 'This file is too large', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isAboveMinSize** | ||
|
||
Check that the file is above the minimum file upload size (checked in | ||
bytes) | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileAboveMinSize', [ | ||
'rule' => ['isAboveMinSize', 1024], | ||
'message' => 'This file is too small', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
ImageValidation | ||
^^^^^^^^^^^^^^^ | ||
|
||
**isAboveMinHeight** | ||
|
||
Check that the file is above the minimum height requirement (checked in | ||
pixels) | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileAboveMinHeight', [ | ||
'rule' => ['isAboveMinHeight', 200], | ||
'message' => 'This image should at least be 200px high', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isBelowMaxHeight** | ||
|
||
Check that the file is below the maximum height requirement (checked in | ||
pixels) | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileBelowMaxHeight', [ | ||
'rule' => ['isBelowMaxHeight', 200], | ||
'message' => 'This image should not be higher than 200px', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isAboveMinWidth** | ||
|
||
Check that the file is above the minimum width requirement (checked in | ||
pixels) | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileAboveMinWidth', [ | ||
'rule' => ['isAboveMinWidth', 200], | ||
'message' => 'This image should at least be 200px wide', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> | ||
|
||
**isBelowMaxWidth** | ||
|
||
Check that the file is below the maximum width requirement (checked in | ||
pixels) | ||
|
||
.. code:: php | ||
|
||
<?php | ||
|
||
$validator->add('file', 'fileBelowMaxWidth', [ | ||
'rule' => ['isBelowMaxWidth', 200], | ||
'message' => 'This image should not be wider than 200px', | ||
'provider' => 'upload' | ||
]); | ||
|
||
?> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Josegonzalez\Upload\Validation; | ||
|
||
use Josegonzalez\Upload\Validation\Traits\ImageValidationTrait; | ||
use Josegonzalez\Upload\Validation\Traits\UploadValidationTrait; | ||
|
||
class DefaultValidation | ||
{ | ||
use ImageValidationTrait; | ||
use UploadValidationTrait; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Josegonzalez\Upload\Validation; | ||
|
||
use Josegonzalez\Upload\Validation\Traits\ImageValidationTrait; | ||
|
||
class ImageValidation | ||
{ | ||
use ImageValidationTrait; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Josegonzalez\Upload\Validation\Traits; | ||
|
||
trait ImageValidationTrait | ||
{ | ||
/** | ||
* Check that the file is above the minimum width requirement | ||
* | ||
* @param mixed $check Value to check | ||
* @param int $width Width of Image | ||
* @return bool Success | ||
*/ | ||
public static function isAboveMinWidth($check, $width) | ||
{ | ||
// Non-file uploads also mean the height is too big | ||
if (!isset($check['tmp_name']) || !strlen($check['tmp_name'])) { | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to return true if there wasn't an image in the first place? The 2.0 version had an option to enable these checks but disable them if there wasn't an upload. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This rule should not be used alone but more in combination with isSuccessfulWrite and mimeType. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we don't allow conditional validation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Conditional validation can be done through a callback on the 'on' parameter: http://book.cakephp.org/3.0/en/core-libraries/validation.html#conditional-validation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please do. |
||
} | ||
list($imgWidth) = getimagesize($check['tmp_name']); | ||
return $width > 0 && $imgWidth >= $width; | ||
} | ||
|
||
/** | ||
* Check that the file is below the maximum width requirement | ||
* | ||
* @param mixed $check Value to check | ||
* @param int $width Width of Image | ||
* @return bool Success | ||
*/ | ||
public static function isBelowMaxWidth($check, $width) | ||
{ | ||
// Non-file uploads also mean the height is too big | ||
if (!isset($check['tmp_name']) || !strlen($check['tmp_name'])) { | ||
return false; | ||
} | ||
list($imgWidth) = getimagesize($check['tmp_name']); | ||
return $width > 0 && $imgWidth <= $width; | ||
} | ||
|
||
/** | ||
* Check that the file is above the minimum height requirement | ||
* | ||
* @param mixed $check Value to check | ||
* @param int $height Height of Image | ||
* @return bool Success | ||
*/ | ||
public static function isAboveMinHeight($check, $height) | ||
{ | ||
// Non-file uploads also mean the height is too big | ||
if (!isset($check['tmp_name']) || !strlen($check['tmp_name'])) { | ||
return false; | ||
} | ||
list(, $imgHeight) = getimagesize($check['tmp_name']); | ||
return $height > 0 && $imgHeight >= $height; | ||
} | ||
|
||
/** | ||
* Check that the file is below the maximum height requirement | ||
* | ||
* @param mixed $check Value to check | ||
* @param int $height Height of Image | ||
* @return bool Success | ||
*/ | ||
public static function isBelowMaxHeight($check, $height) | ||
{ | ||
// Non-file uploads also mean the height is too big | ||
if (!isset($check['tmp_name']) || !strlen($check['tmp_name'])) { | ||
return false; | ||
} | ||
list(, $imgHeight) = getimagesize($check['tmp_name']); | ||
return $height > 0 && $imgHeight <= $height; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the php open/close tags?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how its done in 2.x: https://github.com/josegonzalez/cakephp-upload/blob/2.x/docs/validation.rst