Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to dynamically validate data with Symfony

How to dynamically validate data with Symfony

Symfony Con 2022

Marion Hurteau

November 18, 2022
Tweet

More Decks by Marion Hurteau

Other Decks in Programming

Transcript

  1. How to dynamically validate data with Symfony Marion Hurteau github.com/MarionLeHerisson/validation

    @MarionHerisson / 📧 [email protected] 1
  2. Hello World 👋 Marion Hurteau @MarionHerisson /MarionLeHerisson 📧 [email protected] 󰠈

    Software Engineering @ESGI 󰠁 JoliCode since 2019 2
  3. Hello World 👋 👶 “Disney baby” 󰴛 Nessie & Oscar

    🐈 🖥💻 RPG 3
  4. 4

  5. Validation ? 5

  6. 6

  7. Principle Object Constraint Validator 7

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

  9. Contrainte Validateur Entity Constraint Validator 9

  10. Violations ? 10

  11. The constraints 11

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

    Type, Length 12
  13. Gotta catch’em all ! Email, Regex Url, Hostname, Ip CssColor

    NotCompromisedPassword 13
  14. Gotta catch’em all ! Date, DateTime, Time, Timezone Choice Language,

    Locale, Country File, Image 14
  15. Gotta catch’em all ! Bic, Ibn, CardScheme Isbn, Issn, Isin

    All, AtLeastOneOf, Sequentially 15
  16. Gotta catch’em all ! Compound 16

  17. Gotta catch’em all ! Compound Callback 17

  18. Gotta catch’em all ! Compound Callback Expression 18

  19. Gotta catch’em all ! Compound Callback Expression Valid 19

  20. Going further 20

  21. UniqueEntity 21

  22. Auto-mapping validation 22

  23. Auto-mapping validation 23

  24. Auto-mapping validation 24

  25. Differents ways to validate 25

  26. On a property 26

  27. On a property 27

  28. Inside a Form 28

  29. On a method (a getter or something else) 29

  30. On an entire class 30

  31. On an entire class 31

  32. On an entire class 32

  33. ExecutionContextInterface ? 33

  34. In my code When submitting a form NOT when persisting

    When does it happen ? 34
  35. Custom validation 35

  36. Custom constraint 36

  37. Custom constraint A constraint A validator 37

  38. Custom constraint 38

  39. Dynamic ? 39

  40. Wait, what is a Pokemon ? 40

  41. Wait, what is a Pokemon ? plant fire water 41

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

    42
  43. Wait, what is a Pokemon ? 43

  44. Wait, what is a Pokemon ? 44

  45. Wait, what is a Pokemon? 45

  46. Wait, what is a Pokemon? 46

  47. niv. 18 niv. 36 Use case 47

  48. Use case POST /evolution/create 48

  49. Use case 49

  50. Use case 50

  51. With a Callback 51

  52. Inside the Controller 52

  53. The entity 53

  54. The constraints (in the entity) 54

  55. The constraints (in the entity) 55

  56. The constraints (in the entity) 56

  57. The constraints (in the entity) 57

  58. The constraints (in the entity) 58

  59. The Callback (1/2) 59

  60. The Callback (2/2) 60

  61. Let’s try it ! 61

  62. Let’s try it ! 62

  63. Why is it KO ? 63

  64. Why is it KO ? 64

  65. The Callback (2/2) - Fixed it 65

  66. Let’s try it - again ! 66

  67. Design 👍 Fast 67

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

    👎 Testability 68
  69. 69

  70. Design 👉 Externalise the constraints 👉 DIC 70

  71. With external constraints 71

  72. The Constraint 72

  73. The Constraint 73

  74. Let’s add an interface ✨ 74

  75. Type definition 75

  76. Type definition 76

  77. Add some tags 77

  78. ServiceLocator 78

  79. The ConstraintValidator (1/2) 79

  80. The ConstraintValidator (2/2) 80

  81. 81

  82. TL;DR: 👉 Validate ! ✨ $context->getValidator()->inContext($context) 👉 External constraint :

    - Maintainable - Testable - Reusable 👉 _instanceof & !tagged_locator - + default_index_method 82
  83. 83 Other use cases

  84. Level up: Conditional Constraints 84

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

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

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