[9.x] Adds fluent File
validation rule
#43271
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hello there!
This PR aims to provide a similar experience to the
Password
validation rule for file uploads. It does so by providing a fluent, extendableFile
rule object.Basic usage
Available methods
The following methods are available on the rule:
::types
: equivalent to themimetypes
andmimes
rules::image
: equivalent to theimage
ruleexactly
: equivalent to thesize
rulebetween
: equivalent to thebetween
ruleatLeast
: equivalent to themin
ruleatMost
: equivalent to themax
rulelargerThan
: equivalent to themin
rule + 1kbsmallerThan
: equivalent to themax
rule - 1kbdimensions
: only available after calling::image
, allows specifyingRule::dimensions()
constraintsmimetypes
vsmimes
When using Laravel today, you will either use
mimetypes
ormimes
. Remembering which rule accepts which types can be cumbersome, so theFile::types
method accepts either mimetypes (eg:text/plain
) or file extensions (eg:mp3
) and will intelligently decide on the correct validation rule to use accordingly.It's important to note that if both mimetypes and extensions are used, both
mimetypes
andmimes
validation rules will be present in the request. As such, it is best practice to stick to one approach or the other.Extending for custom types
Whilst the
File
rule only ships with the::image
method as a custom type, the rule isMacroable
, allowing each application to specify more granular file permissions as required.Open for discussion
I've added
default
anddefaults
methods similar to thePassword
rule, but there is something to be said for not including these, as the fact that the class is macroable would basically accomplish the same thing in a more granular fashion. Happy to remove those methods if you can't see a use-case for them :-)Thanks to @nedwors and @owenvoke for ideas and feedback whilst developing this PR.
As always, thanks for all the hard work maintaining and caring for this awesome framework and community 💪
Kind Regards,
Luke