Skip to content

Commit 9fc3165

Browse files
Add form validation to transferPackageAction()
1 parent 62d6807 commit 9fc3165

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

src/Controller/PackageController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,10 @@ public function transferPackageAction(Request $req, #[MapEntity] Package $packag
10191019
$logger->critical($e->getMessage(), ['exception', $e]);
10201020
$this->addFlash('error', 'The package could not be transferred.');
10211021
}
1022+
} elseif (!$form->isValid()) {
1023+
foreach ($form->getErrors(true, true) as $error) {
1024+
$this->addFlash('error', $error->getMessage());
1025+
}
10221026
}
10231027

10241028
return $this->redirectToRoute('view_package', ['name' => $package->getName()]);

src/Form/Model/TransferPackageRequest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
namespace App\Form\Model;
1414

1515
use App\Entity\User;
16+
use App\Validator\Constraints\TransferPackageValidMaintainersList;
1617
use Doctrine\Common\Collections\ArrayCollection;
1718
use Doctrine\Common\Collections\Collection;
1819

1920
class TransferPackageRequest
2021
{
2122
/** @var Collection<int, User> */
23+
#[TransferPackageValidMaintainersList]
2224
private Collection $maintainers;
2325

2426
public function __construct()

src/Form/Type/MaintainerType.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
use App\Entity\User;
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use Symfony\Component\Form\AbstractType;
18+
use Symfony\Component\Form\CallbackTransformer;
19+
use Symfony\Component\Form\Exception\TransformationFailedException;
1820
use Symfony\Component\Form\Extension\Core\Type\TextType;
1921
use Symfony\Component\Form\FormBuilderInterface;
20-
use Symfony\Component\Form\CallbackTransformer;
2122
use Symfony\Component\OptionsResolver\OptionsResolver;
2223

2324
/**
@@ -41,7 +42,18 @@ function (?string $username): ?User {
4142
return null;
4243
}
4344

44-
return $this->em->getRepository(User::class)->findOneByUsernameOrEmail($username);
45+
$user = $this->em->getRepository(User::class)->findOneByUsernameOrEmail($username);
46+
47+
if ($user === null) {
48+
$failure = new TransformationFailedException(sprintf('User "%s" does not exist.', $username));
49+
$failure->setInvalidMessage('The given "{{ value }}" value is not a valid username or email.', [
50+
'{{ value }}' => $username,
51+
]);
52+
53+
throw $failure;
54+
}
55+
56+
return $user;
4557
}
4658
));
4759
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of Packagist.
5+
*
6+
* (c) Jordi Boggiano <j.boggiano@seld.be>
7+
* Nils Adermann <naderman@naderman.de>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace App\Validator\Constraints;
14+
15+
use Symfony\Component\Validator\Constraint;
16+
17+
#[\Attribute]
18+
class TransferPackageValidMaintainersList extends Constraint
19+
{
20+
public string $emptyMessage = 'At least one maintainer must be specified.';
21+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* This file is part of Packagist.
5+
*
6+
* (c) Jordi Boggiano <j.boggiano@seld.be>
7+
* Nils Adermann <naderman@naderman.de>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace App\Validator\Constraints;
14+
15+
use App\Entity\User;
16+
use App\Form\Model\InvalidMaintainer;
17+
use Doctrine\Common\Collections\Collection;
18+
use Symfony\Component\Validator\Constraint;
19+
use Symfony\Component\Validator\ConstraintValidator;
20+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
21+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
22+
23+
class TransferPackageValidMaintainersListValidator extends ConstraintValidator
24+
{
25+
public function validate(mixed $value, Constraint $constraint): void
26+
{
27+
if (!$constraint instanceof TransferPackageValidMaintainersList) {
28+
throw new UnexpectedTypeException($constraint, TransferPackageValidMaintainersList::class);
29+
}
30+
31+
if (null === $value) {
32+
return;
33+
}
34+
35+
if (!$value instanceof Collection) {
36+
throw new UnexpectedValueException($value, Collection::class);
37+
}
38+
39+
if (!$value->isEmpty()) {
40+
return;
41+
}
42+
43+
$this->context->buildViolation($constraint->emptyMessage)
44+
->addViolation();
45+
}
46+
}

0 commit comments

Comments
 (0)