Slide 1

Slide 1 text

How to dynamically validate data with Symfony Marion Hurteau github.com/MarionLeHerisson/validation @MarionHerisson / 📧 [email protected] 1

Slide 2

Slide 2 text

Hello World 👋 Marion Hurteau @MarionHerisson /MarionLeHerisson 📧 [email protected] 󰠈 Software Engineering @ESGI 󰠁 JoliCode since 2019 2

Slide 3

Slide 3 text

Hello World 👋 👶 “Disney baby” 󰴛 Nessie & Oscar 🐈 🖥💻 RPG 3

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

Validation ? 5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

Principle Object Constraint Validator 7

Slide 8

Slide 8 text

Principle Object Constraint Validator ✅ Violation[ ] ❌ Violation[...] 8

Slide 9

Slide 9 text

Contrainte Validateur Entity Constraint Validator 9

Slide 10

Slide 10 text

Violations ? 10

Slide 11

Slide 11 text

The constraints 11

Slide 12

Slide 12 text

Gotta catch’em all ! Blank, NotBlank IsNull, NotNull IsTrue, IsFalse Type, Length 12

Slide 13

Slide 13 text

Gotta catch’em all ! Email, Regex Url, Hostname, Ip CssColor NotCompromisedPassword 13

Slide 14

Slide 14 text

Gotta catch’em all ! Date, DateTime, Time, Timezone Choice Language, Locale, Country File, Image 14

Slide 15

Slide 15 text

Gotta catch’em all ! Bic, Ibn, CardScheme Isbn, Issn, Isin All, AtLeastOneOf, Sequentially 15

Slide 16

Slide 16 text

Gotta catch’em all ! Compound 16

Slide 17

Slide 17 text

Gotta catch’em all ! Compound Callback 17

Slide 18

Slide 18 text

Gotta catch’em all ! Compound Callback Expression 18

Slide 19

Slide 19 text

Gotta catch’em all ! Compound Callback Expression Valid 19

Slide 20

Slide 20 text

Going further 20

Slide 21

Slide 21 text

UniqueEntity 21

Slide 22

Slide 22 text

Auto-mapping validation 22

Slide 23

Slide 23 text

Auto-mapping validation 23

Slide 24

Slide 24 text

Auto-mapping validation 24

Slide 25

Slide 25 text

Differents ways to validate 25

Slide 26

Slide 26 text

On a property 26

Slide 27

Slide 27 text

On a property 27

Slide 28

Slide 28 text

Inside a Form 28

Slide 29

Slide 29 text

On a method (a getter or something else) 29

Slide 30

Slide 30 text

On an entire class 30

Slide 31

Slide 31 text

On an entire class 31

Slide 32

Slide 32 text

On an entire class 32

Slide 33

Slide 33 text

ExecutionContextInterface ? 33

Slide 34

Slide 34 text

In my code When submitting a form NOT when persisting When does it happen ? 34

Slide 35

Slide 35 text

Custom validation 35

Slide 36

Slide 36 text

Custom constraint 36

Slide 37

Slide 37 text

Custom constraint A constraint A validator 37

Slide 38

Slide 38 text

Custom constraint 38

Slide 39

Slide 39 text

Dynamic ? 39

Slide 40

Slide 40 text

Wait, what is a Pokemon ? 40

Slide 41

Slide 41 text

Wait, what is a Pokemon ? plant fire water 41

Slide 42

Slide 42 text

Wait, what is a Pokemon ? lvl. 18 lvl. 36 42

Slide 43

Slide 43 text

Wait, what is a Pokemon ? 43

Slide 44

Slide 44 text

Wait, what is a Pokemon ? 44

Slide 45

Slide 45 text

Wait, what is a Pokemon? 45

Slide 46

Slide 46 text

Wait, what is a Pokemon? 46

Slide 47

Slide 47 text

niv. 18 niv. 36 Use case 47

Slide 48

Slide 48 text

Use case POST /evolution/create 48

Slide 49

Slide 49 text

Use case 49

Slide 50

Slide 50 text

Use case 50

Slide 51

Slide 51 text

With a Callback 51

Slide 52

Slide 52 text

Inside the Controller 52

Slide 53

Slide 53 text

The entity 53

Slide 54

Slide 54 text

The constraints (in the entity) 54

Slide 55

Slide 55 text

The constraints (in the entity) 55

Slide 56

Slide 56 text

The constraints (in the entity) 56

Slide 57

Slide 57 text

The constraints (in the entity) 57

Slide 58

Slide 58 text

The constraints (in the entity) 58

Slide 59

Slide 59 text

The Callback (1/2) 59

Slide 60

Slide 60 text

The Callback (2/2) 60

Slide 61

Slide 61 text

Let’s try it ! 61

Slide 62

Slide 62 text

Let’s try it ! 62

Slide 63

Slide 63 text

Why is it KO ? 63

Slide 64

Slide 64 text

Why is it KO ? 64

Slide 65

Slide 65 text

The Callback (2/2) - Fixed it 65

Slide 66

Slide 66 text

Let’s try it - again ! 66

Slide 67

Slide 67 text

Design 👍 Fast 67

Slide 68

Slide 68 text

Design 👍 Fast 👎 Reusability 👎 Method’s or class size 👎 Testability 68

Slide 69

Slide 69 text

69

Slide 70

Slide 70 text

Design 👉 Externalise the constraints 👉 DIC 70

Slide 71

Slide 71 text

With external constraints 71

Slide 72

Slide 72 text

The Constraint 72

Slide 73

Slide 73 text

The Constraint 73

Slide 74

Slide 74 text

Let’s add an interface ✨ 74

Slide 75

Slide 75 text

Type definition 75

Slide 76

Slide 76 text

Type definition 76

Slide 77

Slide 77 text

Add some tags 77

Slide 78

Slide 78 text

ServiceLocator 78

Slide 79

Slide 79 text

The ConstraintValidator (1/2) 79

Slide 80

Slide 80 text

The ConstraintValidator (2/2) 80

Slide 81

Slide 81 text

81

Slide 82

Slide 82 text

TL;DR: 👉 Validate ! ✨ $context->getValidator()->inContext($context) 👉 External constraint : - Maintainable - Testable - Reusable 👉 _instanceof & !tagged_locator - + default_index_method 82

Slide 83

Slide 83 text

83 Other use cases

Slide 84

Slide 84 text

Level up: Conditional Constraints 84

Slide 85

Slide 85 text

Thank you! @MarionHerisson /MarionLeHerisson/validation 📧 [email protected] 85

Slide 86

Slide 86 text

Questions? 86 @MarionHerisson /MarionLeHerisson/validation 📧 [email protected]

Slide 87

Slide 87 text

https://lparchive.org/Pokemon-Emerald-(by-Crosspei ce)/Update%2034/ 87