inline_validation.rst 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Please note, this solution rely on the validator component so validation defined through
  34. the validator component will be used.
  35. Using this validator
  36. --------------------
  37. Just add the ``InlineConstraint`` class constraint, like this :
  38. .. code-block:: xml
  39. <class name="Application\Sonata\PageBundle\Entity\Block">
  40. <constraint name="Sonata\AdminBundle\Validator\Constraints\InlineConstraint">
  41. <option name="service">sonata.page.cms.page</option>
  42. <option name="method">validateBlock</option>
  43. </constraint>
  44. </class>
  45. There are two important options :
  46. - ``service`` : the service where the validation method is defined
  47. - ``method`` : the service's method to call
  48. The method must accept two arguments :
  49. - ``ErrorElement`` : the instance where assertion can be check
  50. - ``value`` : the object instance
  51. Sample with the ``PageBundle``
  52. ------------------------------
  53. .. code-block:: php
  54. <?php
  55. namespace Sonata\PageBundle\Block;
  56. use Sonata\PageBundle\Model\PageInterface;
  57. use Sonata\AdminBundle\Validator\ErrorElement;
  58. class RssBlockService extends BaseBlockService
  59. {
  60. // ... code removed for simplification
  61. function validateBlock(ErrorElement $errorElement, BlockInterface $block)
  62. {
  63. $errorElement
  64. ->with('settings.url')
  65. ->assertNotNull(array())
  66. ->assertNotBlank()
  67. ->end()
  68. ->with('settings.title')
  69. ->assertNotNull(array())
  70. ->assertNotBlank()
  71. ->assertMinLength(array('limit' => 50))
  72. ->addViolation('ho yeah!')
  73. ->end();
  74. }
  75. }