Field.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\UI\Traits;
  3. use \ThurData\Servers\KerioEmail\Core\UI\Interfaces\BaseValidatorInterface;
  4. use \ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Validators as FieldsValidators;
  5. /**
  6. * Fields related functions
  7. *
  8. * @autor ThurData <info@thrudata.ch>
  9. */
  10. trait Field
  11. {
  12. protected $fieldData = [];
  13. protected $value = null;
  14. protected $defaultValue = null;
  15. protected $description = null;
  16. protected $disabled = false;
  17. protected $validators = [];
  18. protected $width = 8;
  19. protected $labelWidth = 4;
  20. protected $placeholder = null;
  21. protected $validationErrors = [];
  22. public function setDescription($description)
  23. {
  24. $this->description = $description;
  25. return $this;
  26. }
  27. public function getDescription()
  28. {
  29. return $this->description;
  30. }
  31. public function setValue($value)
  32. {
  33. $this->value = $value;
  34. return $this;
  35. }
  36. public function getValue()
  37. {
  38. if ($this->value !== null)
  39. {
  40. return $this->value;
  41. }
  42. return $this->defaultValue;
  43. }
  44. protected function setFieldData($data)
  45. {
  46. $this->fieldData = $data;
  47. return $this;
  48. }
  49. public function getFieldData()
  50. {
  51. return $this->fieldData;
  52. }
  53. public function disableField()
  54. {
  55. $this->disabled = true;
  56. return $this;
  57. }
  58. protected function enableField()
  59. {
  60. $this->disabled = false;
  61. return $this;
  62. }
  63. public function isDisabled()
  64. {
  65. return $this->disabled;
  66. }
  67. public function getValidationErrors()
  68. {
  69. return $this->validationErrors;
  70. }
  71. /*
  72. * adds validator for form field
  73. *
  74. * @param $validator \ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\Validators\BaseValidator
  75. * return $this
  76. */
  77. public function addValidator($validator)
  78. {
  79. if (is_string($validator))
  80. {
  81. $validator = new $validator();
  82. }
  83. if ($validator instanceof BaseValidatorInterface)
  84. {
  85. $this->validators[] = $validator;
  86. }
  87. return $this;
  88. }
  89. /*
  90. * checks if provided data is proper for the form field
  91. *
  92. * return boolen
  93. */
  94. public function isValueValid($data, $additionalData = null)
  95. {
  96. foreach ($this->validators as $validator)
  97. {
  98. if ($validator->isValid($data, $additionalData))
  99. {
  100. continue;
  101. }
  102. $this->validationErrors = array_merge($this->validationErrors, $validator->getErrorsList());
  103. }
  104. if (count($this->validationErrors) > 0)
  105. {
  106. return false;
  107. }
  108. return true;
  109. }
  110. public function getWidth()
  111. {
  112. return $this->width;
  113. }
  114. public function setWidth($width)
  115. {
  116. if ((int) $width > 0)
  117. {
  118. $this->width = (int) $width;
  119. }
  120. return $this;
  121. }
  122. public function getLabelWidth()
  123. {
  124. return $this->labelWidth;
  125. }
  126. public function setLabelWidth($width)
  127. {
  128. if ((int) $width > 0)
  129. {
  130. $this->labelWidth = (int) $width;
  131. }
  132. return $this;
  133. }
  134. public function setPlaceholder($placeholder)
  135. {
  136. $this->placeholder = $placeholder;
  137. return $this;
  138. }
  139. public function getPlaceholder()
  140. {
  141. return $this->placeholder;
  142. }
  143. public function notEmpty()
  144. {
  145. $this->addValidator(new FieldsValidators\NotEmpty());
  146. return $this;
  147. }
  148. public function isIntNumberBetween($min = 0, $max = 0)
  149. {
  150. $this->addValidator(new FieldsValidators\IsIntNumberBetween($min, $max));
  151. return $this;
  152. }
  153. public function setDecimal($mValue = null, $dValue = null)
  154. {
  155. $this->addValidator(new FieldsValidators\Decimal($mValue, $dValue));
  156. return $this;
  157. }
  158. public function setPricingMinimalValues($vMin = null, $vDisabled = null)
  159. {
  160. $this->addValidator(new FieldsValidators\PricingMinAndDisabled($vMin, $vDisabled));
  161. return $this;
  162. }
  163. public function setDefaultValue($value = null)
  164. {
  165. $this->defaultValue = $value;
  166. return $this;
  167. }
  168. public function getDefaultValue()
  169. {
  170. return $this->defaultValue;
  171. }
  172. public function addGroupName($groupName = null)
  173. {
  174. $addGroupWrapper = false;
  175. if (stripos($this->name, '[]'))
  176. {
  177. $this->name = str_replace('[]', '', $this->name);
  178. $addGroupWrapper = true;
  179. }
  180. if (trim($groupName) && trim($groupName) !== '' && is_string(trim($groupName)) && $this->name)
  181. {
  182. $this->name = trim($groupName) . '[' . $this->name . ']';
  183. }
  184. if ($addGroupWrapper)
  185. {
  186. $this->name = $this->name . '[]';
  187. }
  188. return $this;
  189. }
  190. }