diff --git a/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php b/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php index 972aa69767ddc23fb2c757105e344432bea45166..a6da920c669762428b29ae828301d320e7f153c3 100644 --- a/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php +++ b/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraintValidator.php @@ -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. diff --git a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php index c9a247bf40296c963369f5369ff694ad644104d7..a0e96c2082fede078dcefbe238d72062c6425ff3 100644 --- a/core/modules/file/tests/src/Kernel/FileItemValidationTest.php +++ b/core/modules/file/tests/src/Kernel/FileItemValidationTest.php @@ -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()); } /**