selectobjectfield.class.inc.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // Copyright (C) 2010-2016 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. namespace Combodo\iTop\Form\Field;
  19. use \Closure;
  20. use \DBSearch;
  21. use Combodo\iTop\Form\Validator\NotEmptyExtKeyValidator;
  22. /**
  23. * Description of SelectObjectField
  24. *
  25. */
  26. class SelectObjectField extends Field
  27. {
  28. protected $oSearch;
  29. protected $iMaximumComboLength;
  30. protected $iMinAutoCompleteChars;
  31. public function __construct($sId, Closure $onFinalizeCallback = null)
  32. {
  33. parent::__construct($sId, $onFinalizeCallback);
  34. $this->oSearch = null;
  35. $this->iMaximumComboLength = null;
  36. $this->iMinAutoCompleteChars = 3;
  37. }
  38. public function SetSearch(DBSearch $oSearch)
  39. {
  40. $this->oSearch = $oSearch;
  41. }
  42. public function SetMaximumComboLength($iMaximumComboLength)
  43. {
  44. $this->iMaximumComboLength = $iMaximumComboLength;
  45. }
  46. public function SetMinAutoCompleteChars($iMinAutoCompleteChars)
  47. {
  48. $this->iMinAutoCompleteChars = $iMinAutoCompleteChars;
  49. }
  50. /**
  51. * Sets if the field is mandatory or not.
  52. * Setting the value will automatically add/remove a MandatoryValidator to the Field
  53. *
  54. * @param boolean $bMandatory
  55. * @return \Combodo\iTop\Form\Field\Field
  56. */
  57. public function SetMandatory($bMandatory)
  58. {
  59. // Before changing the property, we check if it was already mandatory. If not, we had the mandatory validator
  60. if ($bMandatory && !$this->bMandatory)
  61. {
  62. $this->AddValidator(new NotEmptyExtKeyValidator());
  63. }
  64. if (!$bMandatory)
  65. {
  66. foreach ($this->aValidators as $iKey => $oValue)
  67. {
  68. if ($oValue::Getname() === NotEmptyExtKeyValidator::GetName())
  69. {
  70. unset($this->aValidators[$iKey]);
  71. }
  72. }
  73. }
  74. $this->bMandatory = $bMandatory;
  75. return $this;
  76. }
  77. public function GetSearch()
  78. {
  79. return $this->oSearch;
  80. }
  81. public function GetMaximumComboLength()
  82. {
  83. return $this->iMaximumComboLength;
  84. }
  85. public function GetMinAutoCompleteChars()
  86. {
  87. return $this->iMinAutoCompleteChars;
  88. }
  89. }