Skip to content

Commit

Permalink
Merge pull request #60 from Icinga/bugfix/do-not-always-render-err-ev…
Browse files Browse the repository at this point in the history
…en-with-correct-select-options

Don't always render error messages even with correct select options
  • Loading branch information
nilmerg committed Feb 9, 2022
2 parents 1257dc4 + 15d3107 commit 3dcd341
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/FormElement/FormElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,22 @@ public function getPopulatedValue($name, $default = null)
: $default;
}

/**
* Clear populated value of the given element
*
* @param string $name
*
* @return $this
*/
public function clearPopulatedValue($name)
{
if (! $this->hasBeenSubmitted() && isset($this->populatedValues[$name])) {
unset($this->populatedValues[$name]);
}

return $this;
}

/**
* Add all elements from the given element collection
*
Expand Down
9 changes: 7 additions & 2 deletions src/FormElement/SelectElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ public function hasOption($value)
public function validate()
{
$value = $this->getValue();
if (! ($option = $this->getOption($value)) || $option->getAttributes()->has('disabled')) {
if (
$value !== null && (
! ($option = $this->getOption($value))
|| $option->getAttributes()->has('disabled')
)
) {
$this->valid = false;
$this->addMessage("'$value' is not allowed here");
} elseif ($this->isRequired() && strlen($value) === 0) {
} elseif ($this->isRequired() && $value === null) {
$this->valid = false;
} else {
parent::validate();
Expand Down

0 comments on commit 3dcd341

Please sign in to comment.