conditional_validation.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. Inline Validation
  2. =================
  3. The inline validation is about delegating model validation to a dedicated service.
  4. The current validation implementation built in the Symfony2 framework is very powerful
  5. as it allows to declare validation on : class, field and getter. However these declaration
  6. can take a while to code for complexe rules. As a rules must be a set of a ``Constraint``
  7. and ``Validator`` instance.
  8. The inline validation try to provide a nice solution by introducting a ``ErrorElement``
  9. object. The object can be use to check assertion against a model :
  10. .. code-block:: php
  11. $errorElement
  12. ->with('settings.url')
  13. ->assertNotNull(array())
  14. ->assertNotBlank()
  15. ->end()
  16. ->with('settings.title')
  17. ->assertNotNull(array())
  18. ->assertNotBlank()
  19. ->assertMinLength(array('limit' => 50))
  20. ->addViolation('ho yeah!')
  21. ->end();
  22. if (/* complex rules */) {
  23. $errorElement->with('value')->addViolation('Fail to check the complex rules')->end()
  24. }
  25. /* conditional validation */
  26. if ($this->getSubject()->getState() == Post::STATUS_ONLINE) {
  27. $errorElement
  28. ->with('enabled')
  29. ->assertNotNull()
  30. ->assertTrue()
  31. ->end();
  32. }
  33. .. note::
  34. This solution rely on the validator component so validation defined through
  35. the validator component will be used.
  36. Using this validator
  37. --------------------
  38. Just add the ``InlineConstraint`` class constraint, like this:
  39. .. code-block:: xml
  40. <class name="Application\Sonata\PageBundle\Entity\Block">
  41. <constraint name="Sonata\AdminBundle\Validator\Constraints\InlineConstraint">
  42. <option name="service">sonata.page.cms.page</option>
  43. <option name="method">validateBlock</option>
  44. </constraint>
  45. </class>
  46. There are two important options:
  47. - ``service``: the service where the validation method is defined
  48. - ``method``: the service's method to call
  49. The method must accept two arguments:
  50. - ``ErrorElement``: the instance where assertion can be check
  51. - ``value``: the object instance
  52. Sample with the ``PageBundle``
  53. ------------------------------
  54. .. code-block:: php
  55. <?php
  56. namespace Sonata\PageBundle\Block;
  57. use Sonata\PageBundle\Model\PageInterface;
  58. use Sonata\AdminBundle\Validator\ErrorElement;
  59. class RssBlockService extends BaseBlockService
  60. {
  61. // ... code removed for simplification
  62. public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
  63. {
  64. $errorElement
  65. ->with('settings.url')
  66. ->assertNotNull(array())
  67. ->assertNotBlank()
  68. ->end()
  69. ->with('settings.title')
  70. ->assertNotNull(array())
  71. ->assertNotBlank()
  72. ->assertMinLength(array('limit' => 50))
  73. ->addViolation('ho yeah!')
  74. ->end();
  75. }
  76. }