@@ -28,6 +28,8 @@ class UploadControl extends BaseControl
2828 /** @deprecated use UploadControl::Valid */
2929 public const VALID = self ::Valid;
3030
31+ private bool $ nullable = false ;
32+
3133
3234 public function __construct (string |Stringable |null $ label = null , bool $ multiple = false )
3335 {
@@ -55,7 +57,6 @@ public function __construct(string|Stringable|null $label = null, bool $multiple
5557 public function loadHttpData (): void
5658 {
5759 $ this ->value = $ this ->getHttpData (Form::DataFile);
58- $ this ->value ??= new FileUpload (null );
5960 }
6061
6162
@@ -75,25 +76,48 @@ public function setValue($value)
7576 }
7677
7778
79+ public function getValue (): FileUpload |array |null
80+ {
81+ return $ this ->value ?? ($ this ->nullable ? null : new FileUpload (null ));
82+ }
83+
84+
7885 /**
7986 * Has been any file uploaded?
8087 */
8188 public function isFilled (): bool
8289 {
83- return $ this ->value instanceof FileUpload
84- ? $ this ->value ->getError () !== UPLOAD_ERR_NO_FILE // ignore null object
85- : (bool ) $ this ->value ;
90+ return (bool ) $ this ->value ;
91+ }
92+
93+
94+ /**
95+ * Sets whether getValue() returns null instead of FileUpload with error UPLOAD_ERR_NO_FILE.
96+ */
97+ public function setNullable (bool $ value = true ): static
98+ {
99+ $ this ->nullable = $ value ;
100+ return $ this ;
101+ }
102+
103+
104+ public function isNullable (): bool
105+ {
106+ return $ this ->nullable ;
86107 }
87108
88109
89110 /**
90111 * Have been all files successfully uploaded?
112+ * @internal
91113 */
92114 public function isOk (): bool
93115 {
94- return $ this ->value instanceof FileUpload
95- ? $ this ->value ->isOk ()
96- : $ this ->value && Arrays::every ($ this ->value , fn (FileUpload $ upload ): bool => $ upload ->isOk ());
116+ return $ this ->value &&
117+ Arrays::every (
118+ $ this ->value instanceof FileUpload ? [$ this ->value ] : $ this ->value ,
119+ fn (FileUpload $ upload ): bool => $ upload ->isOk ()
120+ );
97121 }
98122
99123
0 commit comments