8000 [Validator] Improvement: provide file basename for constr. violation messages in FileValidator. by TheCelavi · Pull Request #26261 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Improvement: provide file basename for constr. violation messages in FileValidator. #26261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Validator] Improvement: provide file basename for constr. violation …
…messages in FileValidator.
  • Loading branch information
TheCelavi authored and fabpot committed Oct 10, 2018
commit a77abadf06b9f3061a4c0977a9ea588c258008e8
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ public function validate($value, Constraint $constraint)
}

$sizeInBytes = filesize($path);
$basename = $value instanceof UploadedFile ? $value->getClientOriginalName() : basename($path);

if (0 === $sizeInBytes) {
$this->context->buildViolation($constraint->disallowEmptyMessage)
->setParameter('{{ file }}', $this->formatValue($path))
->setParameter('{{ name }}', $this->formatValue($basename))
->setCode(File::EMPTY_ERROR)
->addViolation();

Expand All @@ -158,6 +160,7 @@ public function validate($value, Constraint $constraint)
->setParameter('{{ size }}', $sizeAsString)
->setParameter('{{ limit }}', $limitAsString)
->setParameter('{{ suffix }}', $suffix)
->setParameter('{{ name }}', $this->formatValue($basename))
->setCode(File::TOO_LARGE_ERROR)
->addViolation();

Expand Down Expand Up @@ -189,6 +192,7 @@ public function validate($value, Constraint $constraint)
->setParameter('{{ file }}', $this->formatValue($path))
->setParameter('{{ type }}', $this->formatValue($mime))
->setParameter('{{ types }}', $this->formatValues($mimeTypes))
->setParameter('{{ name }}', $this->formatValue($basename))
->setCode(File::INVALID_MIME_TYPE_ERROR)
->addViolation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public function testMaxSizeExceeded($bytesWritten, $limit, $sizeAsString, $limit
->setParameter('{{ size }}', $sizeAsString)
->setParameter('{{ suffix }}', $suffix)
->setParameter('{{ file }}', '"'.$this->path.'"')
->setParameter('{{ name }}', '"'.basename($this->path).'"')
->setCode(File::TOO_LARGE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -278,6 +279,7 @@ public function testBinaryFormat($bytesWritten, $limit, $binaryFormat, $sizeAsSt
->setParameter('{{ size }}', $sizeAsString)
->setParameter('{{ suffix }}', $suffix)
->setParameter('{{ file }}', '"'.$this->path.'"')
->setParameter('{{ name }}', '"'.basename($this->path).'"')
->setCode(File::TOO_LARGE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -356,6 +358,7 @@ public function testInvalidMimeType()
->setParameter('{{ type }}', '"application/pdf"')
->setParameter('{{ types }}', '"image/png", "image/jpg"')
->setParameter('{{ file }}', '"'.$this->path.'"')
->setParameter('{{ name }}', '"'.basename($this->path).'"')
->setCode(File::INVALID_MIME_TYPE_ERROR)
->assertRaised();
}
Expand Down Expand Up @@ -386,6 +389,7 @@ public function testInvalidWildcardMimeType()
->setParameter('{{ type }}', '"application/pdf"')
->setParameter('{{ types }}', '"image/*", "image/jpg"')
->setParameter('{{ file }}', '"'.$this->path.'"')
->setParameter('{{ name }}', '"'.basename($this->path).'"')
->setCode(File::INVALID_MIME_TYPE_ERROR)
->assertRaised();
}
Expand All @@ -402,6 +406,7 @@ public function testDisallowEmpty()

$this->buildViolation('myMessage')
->setParameter('{{ file }}', '"'.$this->path.'"')
->setParameter('{{ name }}', '"'.basename($this->path).'"')
->setCode(File::EMPTY_ERROR)
->assertRaised();
}
Expand Down
0