IpAddressValidator.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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\Servers\ProxmoxVps\App\UI\Validators;
  20. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  21. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Validators\BaseValidator;
  22. /**
  23. * Description of NumberValidator
  24. *
  25. * @author Pawel Kopec <pawelk@modulesgardne.com>
  26. */
  27. class IpAddressValidator extends BaseValidator
  28. {
  29. protected $required = false;
  30. public function __construct($required = false)
  31. {
  32. $this->required = $required;
  33. }
  34. protected function validate($data, $additionalData = null)
  35. {
  36. if (!$this->required && empty($data))
  37. {
  38. return true;
  39. }
  40. if (!filter_var($data, FILTER_VALIDATE_IP))
  41. {
  42. $this->addValidationError('IP Address is not valid', false);
  43. return false;
  44. }
  45. if ($this->required && VmIpAddress::ofIp($data)->count())
  46. {
  47. $this->addValidationError('IP Address already exist', false);
  48. return false;
  49. }
  50. return true;
  51. }
  52. }