Unverified Commit 37b9e45a authored by alexpott's avatar alexpott

Issue #2956814 by tim.plunkett: FileValidationConstraintValidator assumes that the file exists

parent d487aa13
......@@ -15,7 +15,12 @@ class FileValidationConstraintValidator extends ConstraintValidator {
*/
public function validate($value, Constraint $constraint) {
// Get the file to execute validators.
$file = $value->get('entity')->getTarget()->getValue();
$target = $value->get('entity')->getTarget();
if (!$target) {
return;
}
$file = $target->getValue();
// Get the validators.
$validators = $value->getUploadValidators();
// Checks that a file meets the criteria specified by the validators.
......
......@@ -104,6 +104,18 @@ public function testFileValidationConstraint($file_type) {
$this->assertEquals('The file is <em class="placeholder">2.93 KB</em> exceeding the maximum file size of <em class="placeholder">2 KB</em>.', (string) $result->get(0)->getMessage());
$this->assertEquals('field_test_file.0', $result->get(1)->getPropertyPath());
$this->assertEquals('Only files with the following extensions are allowed: <em class="placeholder">jpg|png</em>.', (string) $result->get(1)->getMessage());
// Refer to a file that does not exist.
$entity_test = EntityTest::create([
'uid' => $this->user->id(),
'field_test_file' => [
'target_id' => 2,
],
]);
$result = $entity_test->validate();
$this->assertCount(1, $result);
$this->assertEquals('field_test_file.0.target_id', $result->get(0)->getPropertyPath());
$this->assertEquals('The referenced entity (<em class="placeholder">file</em>: <em class="placeholder">2</em>) does not exist.', (string) $result->get(0)->getMessage());
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment