DiskSizeValidator.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Validators;
  3. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
  4. use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Validators\BaseValidator;
  5. class DiskSizeValidator extends BaseValidator
  6. {
  7. protected $additionalField;
  8. protected $minValue = 0;
  9. protected $maxValue = 0;
  10. protected $required = true;
  11. public function __construct( $additionalField, $required = true)
  12. {
  13. $this->additionalField = $additionalField;
  14. $this->required = $required;
  15. }
  16. protected function validate($data, $additionalData = null)
  17. {
  18. if (!$this->required && empty($data))
  19. {
  20. return true;
  21. }
  22. $resurceManager = new ResourceManager();
  23. $additionalSize = 0;
  24. if($additionalData->get('formData')[$this->additionalField] ){
  25. $additionalSize = (int) $additionalData->get('formData')[$this->additionalField];
  26. }
  27. $diskResource = $resurceManager ->disk();
  28. $diskResource->setTotal($diskResource->getTotal()-$additionalSize);
  29. $this->maxValue = $diskResource->free();
  30. $this->minValue = $diskResource->getMin();
  31. logModuleCall(
  32. 'proxmoxCloud',
  33. __FUNCTION__,
  34. $diskResource,
  35. 'Debug',
  36. $data
  37. );
  38. if(preg_match("/\./", $data)){
  39. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  40. return false;
  41. }
  42. if (is_numeric($data) && $this->minValue === 0 && $this->maxValue === 0)
  43. {
  44. return true;
  45. }
  46. //Min & Max
  47. if (is_numeric($data) && $this->minValue <= ((int)$data) && ((int)$data) <= $this->maxValue)
  48. {
  49. return true;
  50. }
  51. //Min
  52. else
  53. {
  54. if (is_numeric($data) && !is_numeric($this->maxValue) && $this->minValue <= ((int)$data))
  55. {
  56. return true;
  57. }
  58. }
  59. if ($this->minValue === $this->maxValue)
  60. {
  61. $this->addValidationError('PleaseProvideANumericValue');
  62. return false;
  63. }
  64. if (is_numeric($this->minValue) && is_numeric($this->maxValue))
  65. {
  66. $this->addValidationError('PleaseProvideANumericValueBetween', false, ['minValue' => $this->minValue, 'maxValue' => $this->maxValue]);
  67. }
  68. else
  69. {
  70. if (is_numeric($this->minValue) && !is_numeric($this->maxValue))
  71. {
  72. $this->addValidationError('PleaseProvideANumericValueFrom', false, ['minValue' => $this->minValue]);
  73. }
  74. }
  75. return false;
  76. }
  77. }