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:
Rafael Dohms 2012-03-19 14:37:25 +01:00
parent edac48a824
commit ac2a187b4d
3 changed files with 33 additions and 6 deletions

View File

@ -27,7 +27,12 @@ class File extends Constraint
public $maxSizeMessage = 'The file is too large ({{ size }}). Allowed maximum size is {{ limit }}';
public $mimeTypesMessage = 'The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}';
public $uploadIniSizeErrorMessage = 'The file is too large. Allowed maximum size is {{ limit }}';
public $uploadFormSizeErrorMessage = 'The file is too large';
public $uploadErrorMessage = 'The file could not be uploaded';
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';
}

View File

@ -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);

View File

@ -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'),
);
}