conditional_validation.rst 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. Inline Validation
  2. =================
  3. The inline validation code is now part of the SonataCoreBundle. You can refer to the related documentation for more information.
  4. The above examples show how the integration has been done with the SonataAdminBundle. For completeness, it's worth remembering that
  5. the ``Admin`` class itself contains an empty ``validate`` method. This is automatically called, so you can override it in your own admin class:
  6. .. code-block:: php
  7. // add this to your existing use statements
  8. use Sonata\CoreBundle\Validator\ErrorElement;
  9. class MyAdmin extends AbstractAdmin
  10. {
  11. // add this method
  12. public function validate(ErrorElement $errorElement, $object)
  13. {
  14. $errorElement
  15. ->with('name')
  16. ->assertLength(array('max' => 32))
  17. ->end()
  18. ;
  19. }
  20. Troubleshooting
  21. ---------------
  22. Make sure your validator method is being called. If in doubt, try throwing an exception:
  23. .. code-block:: php
  24. public function validate(ErrorElement $errorElement, $object)
  25. {
  26. throw new \Exception(__METHOD__);
  27. }
  28. There should not be any validation_groups defined for the form. If you have code like the example below in
  29. your ``Admin`` class, remove the 'validation_groups' entry, the whole $formOptions property or set validation_groups
  30. to an empty array:
  31. .. code-block:: php
  32. protected $formOptions = array(
  33. 'validation_groups' => array()
  34. );