UrlField.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Form;
  11. /**
  12. * Field for entering URLs.
  13. *
  14. * Available options:
  15. *
  16. * * default_protocol: If specified, {default_protocol}:// (e.g. http://)
  17. * will be prepended onto any input string that
  18. * doesn't begin with the protocol.
  19. *
  20. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  21. */
  22. class UrlField extends TextField
  23. {
  24. /**
  25. * {@inheritDoc}
  26. */
  27. protected function configure()
  28. {
  29. $this->addOption('default_protocol', 'http');
  30. parent::configure();
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. protected function processData($data)
  36. {
  37. $protocol = $this->getOption('default_protocol');
  38. if ($protocol && $data && !preg_match('~^\w+://~', $data)) {
  39. $data = $protocol . '://' . $data;
  40. }
  41. return $data;
  42. }
  43. }