IpAddressAndHostnameValidator.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Aug 23, 2018)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\UI\Validators;
  20. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Validators\BaseValidator;
  21. /**
  22. * Description of NumberValidator
  23. *
  24. * @author Pawel Kopec <pawelk@modulesgardne.com>
  25. */
  26. class IpAddressAndHostnameValidator extends BaseValidator
  27. {
  28. protected $required = false;
  29. protected $public = true;
  30. public function __construct($required = false, $public = true)
  31. {
  32. $this->required = $required;
  33. $this->public = $public;
  34. }
  35. protected function validate($data, $additionalData = null)
  36. {
  37. if (!$this->required && empty($data))
  38. {
  39. return true;
  40. }
  41. $ip = filter_var($data, FILTER_VALIDATE_IP);
  42. $url = filter_var($data, FILTER_VALIDATE_URL);
  43. $host = filter_var($data, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
  44. if ( !$ip && !$url && !$host)
  45. {
  46. $this->addValidationError('The value is not valid', false);
  47. return false;
  48. }
  49. return true;
  50. }
  51. }