• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13724522058

07 Mar 2025 04:04PM UTC coverage: 8.175% (-0.3%) from 8.518%
13724522058

Pull #7005

github

web-flow
Merge 322407532 into 1e0bc9dc8
Pull Request #7005: fix(validation): deprecate string message for ValidationException con…

4 of 6 new or added lines in 1 file covered. (66.67%)

159 existing lines in 24 files now uncovered.

12839 of 157045 relevant lines covered (8.18%)

13.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/docs/guides/delete-operation-with-validation.php
1
<?php
2
// ---
3
// slug: delete-operation-with-validation
4
// name: Delete operation with validation
5
// position: 20
6
// tags: validation, expert
7
// executable: true
8
// ---
9

10
// Let's add a [custom Constraint](https://symfony.com/doc/current/validation/custom_constraint.html).
11

12
namespace App\Validator {
13
    use Symfony\Component\Validator\Constraint;
14

15
    #[\Attribute]
16
    class AssertCanDelete extends Constraint
17
    {
18
        public string $message = 'For whatever reason we denied removal of this data.';
19
        public string $mode = 'strict';
20

21
        public function getTargets(): string
22
        {
23
            return self::CLASS_CONSTRAINT;
×
24
        }
25
    }
26
}
27

28
// And a custom validator following Symfony's naming conventions.
29

30
namespace App\Validator {
31
    use Symfony\Component\Validator\Constraint;
32
    use Symfony\Component\Validator\ConstraintValidator;
33

34
    class AssertCanDeleteValidator extends ConstraintValidator
35
    {
36
        public function validate(mixed $value, Constraint $constraint): void
37
        {
38
            $this->context->buildViolation($constraint->message)->addViolation();
×
39
        }
40
    }
41
}
42

43
namespace App\Entity {
44
    use ApiPlatform\Metadata\Delete;
45
    use ApiPlatform\Validator\Exception\ValidationException;
46
    use App\Validator\AssertCanDelete;
47
    use Doctrine\ORM\Mapping as ORM;
48

49
    #[ORM\Entity]
50
    #[Delete(
51
        // By default, validation is not triggered on a DELETE operation, let's activate it.
52
        validate: true,
×
53
        // Just as with serialization we can add [validation groups](/docs/core/validation/#using-validation-groups).
54
        validationContext: ['groups' => ['deleteValidation']],
×
55
        exceptionToStatus: [ValidationException::class => 403]
×
56
    )]
×
57
    // Here we use the previously created constraint on the class directly.
58
    #[AssertCanDelete(groups: ['deleteValidation'])]
59
    class Book
60
    {
61
        #[ORM\Id, ORM\Column, ORM\GeneratedValue]
62
        private ?int $id = null;
63

64
        #[ORM\Column]
65
        public string $title = '';
66

67
        public function getId()
68
        {
69
            return $this->id;
×
70
        }
71
    }
72
}
73

74
namespace App\Playground {
75
    use Symfony\Component\HttpFoundation\Request;
76

77
    function request(): Request
78
    {
79
        return Request::create(uri: '/books/1', method: 'DELETE', server: ['CONTENT_TYPE' => 'application/ld+json']);
×
80
    }
81
}
82

83
namespace App\Fixtures {
84
    use App\Entity\Book;
85
    use Doctrine\Bundle\FixturesBundle\Fixture;
86
    use Doctrine\Persistence\ObjectManager;
87

88
    use function Zenstruck\Foundry\anonymous;
89
    use function Zenstruck\Foundry\faker;
90
    use function Zenstruck\Foundry\repository;
91

92
    final class BookFixtures extends Fixture
93
    {
94
        public function load(ObjectManager $manager): void
95
        {
96
            $bookFactory = anonymous(Book::class);
×
97
            if (repository(Book::class)->count()) {
×
98
                return;
×
99
            }
100

101
            $bookFactory->many(10)->create(
×
102
                fn () => [
×
103
                    'title' => faker()->name(),
×
104
                ]
×
105
            );
×
106
        }
107
    }
108
}
109

110
namespace DoctrineMigrations {
111
    use Doctrine\DBAL\Schema\Schema;
112
    use Doctrine\Migrations\AbstractMigration;
113

114
    final class Migration extends AbstractMigration
115
    {
116
        public function up(Schema $schema): void
117
        {
118
            $this->addSql('CREATE TABLE book (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title VARCHAR(255) NOT NULL)');
×
119
        }
120
    }
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc