Skip to content
Snippets Groups Projects
Unverified Commit 37b9e45a authored by Alex Pott's avatar Alex Pott
Browse files

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

parent d487aa13
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -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());
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment