There was such a task: to make mandatory the selection of a file in the form .
Writing in the rules validator required everything seems to work as expected:
[['file'], 'required'], [['file'], 'file', 'maxFiles' => 5], That is, on the client, both the “mandatory selection” and the “maximum allowed number of files” are triggered and the server skips everything.
Later (surfing the documentation) I found in the yii \ validators \ Validator class, from which other validators inherit, the $skipOnEmpty property:
If this attribute value is null or an empty string.
Determines whether the current validation rule will be skipped if the attribute value is
nullor an empty string.
The default is true .
Reading the documentation further, I also found the $uploadRequired property, but it was in yii\validators\FileValidator :
The error message used when no file is uploaded. Note: This is the text of the validation error message. To make uploading files required, you have to set $ skipOnEmpty to false.
This error message is used when the file is loaded. Note that this is the text of the validation error message. To make files required for downloading, you must set
$skipOnEmptytofalse.
I tried to use the skipOnEmpty property to false instead of the validator required :
//[['file'], 'required'], [['file'], 'file', 'maxFiles' => 5, 'skipOnEmpty' => false], It turned out that on the client the file really becomes mandatory for downloading; BUT after selecting a file and sending the form, it does not pass validation on the file: it says that the file type is not selected (Please upload a file) ...
Why is this happening: the client needs to choose - we choose - the client skips, and the server says that the file is not selected?
Well, based on the above question: is it correct to indicate the mandatory download of the file using the required validator?