troubleshooting.rst 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. Troubleshooting
  2. ===============
  3. The toString method
  4. -------------------
  5. Sometimes the bundle needs to display your model objects, in order to do it,
  6. objects are converted to string by using the `__toString`_ magic method.
  7. Take care to never return anything else than a string in this method.
  8. For example, if your method looks like that :
  9. .. code-block:: php
  10. public function __toString()
  11. {
  12. return $this->getTitle();
  13. }
  14. You cannot be sure your object will *always* have a title when the bundle will want to convert it to a string.
  15. So in order to avoid any fatal error, you must return an empty string
  16. (or anything you prefer) for when the title is missing, like this :
  17. .. code-block:: php
  18. public function __toString()
  19. {
  20. return $this->getTitle() ?: '';
  21. }
  22. .. _`__toString`: http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
  23. Large filters and long urls problem
  24. -----------------------------------
  25. If you will try to add hundreds of filters to a single admin class, you will get a problem - very long generated filter form url.
  26. In most cases you will get server response like *Error 400 Bad Request* OR *Error 414 Request-URI Too Long*. According to
  27. `a StackOverflow discussion <http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers>`_
  28. "safe" url length is just around 2000 characters.
  29. You can fix this issue by adding a simple JQuery piece of code on your edit template :
  30. .. code-block:: javascript
  31. $(function() {
  32. // Add class 'had-value-on-load' to inputs/selects with values.
  33. $(".sonata-filter-form input").add(".sonata-filter-form select").each(function(){
  34. if($(this).val()) {
  35. $(this).addClass('had-value-on-load');
  36. }
  37. });
  38. // REMOVE ALL EMPTY INPUT FROM FILTER FORM (except inputs, which has class 'had-value-on-load')
  39. $(".sonata-filter-form").submit(function() {
  40. $(".sonata-filter-form input").add(".sonata-filter-form select").each(function(){
  41. if(!$(this).val() && !$(this).hasClass('had-value-on-load')) {
  42. $(this).remove()
  43. };
  44. });
  45. });
  46. });