conditional_validation.rst 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. <?php
  12. $errorElement
  13. ->with('settings.url')
  14. ->assertNotNull(array())
  15. ->assertNotBlank()
  16. ->end()
  17. ->with('settings.title')
  18. ->assertNotNull(array())
  19. ->assertNotBlank()
  20. ->assertMinLength(array('limit' => 50))
  21. ->addViolation('ho yeah!')
  22. ->end();
  23. if (/* complex rules */) {
  24. $errorElement->with('value')->addViolation('Fail to check the complex rules')->end()
  25. }
  26. /* conditional validation */
  27. if ($this->getSubject()->getState() == Post::STATUS_ONLINE) {
  28. $errorElement
  29. ->with('enabled')
  30. ->assertNotNull()
  31. ->assertTrue()
  32. ->end();
  33. }
  34. .. note::
  35. This solution rely on the validator component so validation defined through
  36. the validator component will be used.
  37. Using this validator
  38. --------------------
  39. Just add the ``InlineConstraint`` class constraint, like this:
  40. .. code-block:: xml
  41. <class name="Application\Sonata\PageBundle\Entity\Block">
  42. <constraint name="Sonata\AdminBundle\Validator\Constraints\InlineConstraint">
  43. <option name="service">sonata.page.cms.page</option>
  44. <option name="method">validateBlock</option>
  45. </constraint>
  46. </class>
  47. There are two important options:
  48. - ``service``: the service where the validation method is defined
  49. - ``method``: the service's method to call
  50. The method must accept two arguments:
  51. - ``ErrorElement``: the instance where assertion can be check
  52. - ``value``: the object instance
  53. Sample with the ``PageBundle``
  54. ------------------------------
  55. .. code-block:: php
  56. <?php
  57. namespace Sonata\PageBundle\Block;
  58. use Sonata\PageBundle\Model\PageInterface;
  59. use Sonata\AdminBundle\Validator\ErrorElement;
  60. class RssBlockService extends BaseBlockService
  61. {
  62. // ... code removed for simplification
  63. public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
  64. {
  65. $errorElement
  66. ->with('settings.url')
  67. ->assertNotNull(array())
  68. ->assertNotBlank()
  69. ->end()
  70. ->with('settings.title')
  71. ->assertNotNull(array())
  72. ->assertNotBlank()
  73. ->assertMinLength(array('limit' => 50))
  74. ->addViolation('ho yeah!')
  75. ->end();
  76. }
  77. }