[9.x] Adds fluent File validation rule#43271
Conversation
|
I think it would be nice (and others such @olivernybroe have mentioned it) to be able to specify the unit (mb, kb) alongside the size. However, I'm thinking that it should likely be part of a follow-up PR to keep this PR's scope as small as possible. Were this to go in, I could see a File::image()->smallerThan(Size::mb(12))->largerThan(Size::kb(512)) |
|
This is super cool! I actually like the idea of defining my standard file upload rules in one place and reusing them. Having a fluent object to do rather than just a PHP array is way better! |
|
Thanks! I simplified this a bit to just |
|
Is it posible make combination rule in one file like this |
Hello there!
This PR aims to provide a similar experience to the
Passwordvalidation rule for file uploads. It does so by providing a fluent, extendableFilerule object.Basic usage
Available methods
The following methods are available on the rule:
::types: equivalent to themimetypesandmimesrules::image: equivalent to theimageruleexactly: equivalent to thesizerulebetween: equivalent to thebetweenruleatLeast: equivalent to theminruleatMost: equivalent to themaxrulelargerThan: equivalent to theminrule + 1kbsmallerThan: equivalent to themaxrule - 1kbdimensions: only available after calling::image, allows specifyingRule::dimensions()constraintsmimetypesvsmimesWhen using Laravel today, you will either use
mimetypesormimes. Remembering which rule accepts which types can be cumbersome, so theFile::typesmethod 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
mimetypesandmimesvalidation 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
Filerule only ships with the::imagemethod as a custom type, the rule isMacroable, allowing each application to specify more granular file permissions as required.Open for discussion
I've added
defaultanddefaultsmethods similar to thePasswordrule, 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