ValidationHelper.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * ValidationHelper.
  11. *
  12. * @package symfony
  13. * @subpackage helper
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: ValidationHelper.php 7757 2008-03-07 10:55:22Z fabien $
  16. */
  17. function form_has_error($param)
  18. {
  19. return sfContext::getInstance()->getRequest()->hasError($param);
  20. }
  21. function form_error($param, $options = array(), $catalogue = 'messages')
  22. {
  23. $param_for_sf = str_replace(array('[', ']'), array('{', '}'), $param);
  24. $param = str_replace(array('{', '}'), array('[', ']'), $param);
  25. $options = _parse_attributes($options);
  26. $context = sfContext::getInstance();
  27. $request = $context->getRequest();
  28. $style = $request->hasError($param_for_sf) ? '' : 'display:none;';
  29. $options['style'] = $style.(isset($options['style']) ? $options['style']:'');
  30. if (!isset($options['class']))
  31. {
  32. $options['class'] = sfConfig::get('sf_validation_error_class', 'form_error');
  33. }
  34. if (!isset($options['id']))
  35. {
  36. $options['id'] = sfConfig::get('sf_validation_error_id_prefix', 'error_for_').get_id_from_name($param);
  37. }
  38. $prefix = sfConfig::get('sf_validation_error_prefix', '');
  39. if (isset($options['prefix']))
  40. {
  41. $prefix = $options['prefix'];
  42. unset($options['prefix']);
  43. }
  44. $suffix = sfConfig::get('sf_validation_error_suffix', '');
  45. if (isset($options['suffix']))
  46. {
  47. $suffix = $options['suffix'];
  48. unset($options['suffix']);
  49. }
  50. // translate error message if needed
  51. $error = $request->getError($param_for_sf);
  52. if (sfConfig::get('sf_i18n'))
  53. {
  54. $error = $context->getI18N()->__($error, null, $catalogue);
  55. }
  56. return content_tag('div', $prefix.$error.$suffix, $options)."\n";
  57. }