Improved feedback for Upload Validator to cover all PHP error states.
This way we don't get a unclear "upload error" message unless its something completely unexpected.
This commit is contained in:
parent
edac48a824
commit
ac2a187b4d
@ -29,5 +29,10 @@ class File extends Constraint
|
||||
|
||||
public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }}';
|
||||
public $uploadFormSizeErrorMessage = 'The file is too large';
|
||||
public $uploadPartialErrorMessage = 'The file was only partially uploaded';
|
||||
public $uploadNoFileErrorMessage = 'No file was uploaded';
|
||||
public $uploadNoTmpDirErrorMessage = 'No temporary folder was configured in php.ini';
|
||||
public $uploadCantWriteErrorMessage = 'Cannot write temporary file to disk';
|
||||
public $uploadExtensionErrorMessage = 'A PHP extension cause the upload to fail';
|
||||
public $uploadErrorMessage = 'The file could not be uploaded';
|
||||
}
|
||||
|
@ -50,6 +50,26 @@ class FileValidator extends ConstraintValidator
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
$this->context->addViolation($constraint->uploadFormSizeErrorMessage);
|
||||
|
||||
return false;
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
$this->context->addViolation($constraint->uploadPartialErrorMessage);
|
||||
|
||||
return false;
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
$this->context->addViolation($constraint->uploadNoFileErrorMessage);
|
||||
|
||||
return false;
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
$this->context->addViolation($constraint->uploadNoTmpDirErrorMessage);
|
||||
|
||||
return false;
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
$this->context->addViolation($constraint->uploadCantWriteErrorMessage);
|
||||
|
||||
return false;
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
$this->context->addViolation($constraint->uploadExtensionErrorMessage);
|
||||
|
||||
return false;
|
||||
default:
|
||||
$this->context->addViolation($constraint->uploadErrorMessage);
|
||||
|
@ -303,9 +303,11 @@ abstract class FileValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
return array(
|
||||
array(UPLOAD_ERR_INI_SIZE, 'uploadIniSizeErrorMessage', array('{{ limit }}' => UploadedFile::getMaxFilesize() . ' bytes')),
|
||||
array(UPLOAD_ERR_FORM_SIZE, 'uploadFormSizeErrorMessage'),
|
||||
array(UPLOAD_ERR_PARTIAL, 'uploadErrorMessage'),
|
||||
array(UPLOAD_ERR_NO_TMP_DIR, 'uploadErrorMessage'),
|
||||
array(UPLOAD_ERR_EXTENSION, 'uploadErrorMessage'),
|
||||
array(UPLOAD_ERR_PARTIAL, 'uploadPartialErrorMessage'),
|
||||
array(UPLOAD_ERR_NO_FILE, 'uploadNoFileErrorMessage'),
|
||||
array(UPLOAD_ERR_NO_TMP_DIR, 'uploadNoTmpDirErrorMessage'),
|
||||
array(UPLOAD_ERR_CANT_WRITE, 'uploadCantWriteErrorMessage'),
|
||||
array(UPLOAD_ERR_EXTENSION, 'uploadExtensionErrorMessage'),
|
||||
);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user