Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default validator for select and multiselect element #83

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions src/FormElement/MultiselectElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,6 @@ public function setValue($value)
return $this;
}

public function validate()
{
foreach ($this->getValue() as $value) {
$option = $this->getOption($value);
if (! $option || $option->getAttributes()->has('disabled')) {
$this->valid = false;
$this->addMessage(sprintf($this->translate("'%s' is not allowed here"), $value));

return $this;
}
}

BaseFormElement::validate();

return $this;
}

protected function isSelectedOption($optionValue)
{
return in_array($optionValue, $this->getValue(), ! is_int($optionValue));
Expand Down
45 changes: 20 additions & 25 deletions src/FormElement/SelectElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace ipl\Html\FormElement;

use Closure;
use ipl\Html\Html;
use ipl\I18n\Translation;
use ipl\Validator\DeferredInArrayValidator;

class SelectElement extends BaseFormElement
{
Expand All @@ -16,6 +18,12 @@ class SelectElement extends BaseFormElement

protected $optionContent = [];

/** @var Closure */
protected $getPossibleValues;

/** @var array Disabled values */
protected $disabledOptions = [];

public function __construct($name, $attributes = null)
{
$this->getAttributes()->registerAttributeCallback(
Expand All @@ -38,26 +46,6 @@ public function hasOption($value)
return isset($this->options[$value]);
}

public function validate()
{
$value = $this->getValue();
if (
$value !== null && (
! ($option = $this->getOption($value))
|| $option->getAttributes()->has('disabled')
)
) {
$this->valid = false;
$this->addMessage(sprintf($this->translate("'%s' is not allowed here"), $value));

return $this;
}

parent::validate();

return $this;
}

public function deselect()
{
$this->setValue(null);
Expand All @@ -67,15 +55,13 @@ public function deselect()

public function disableOption($value)
{
$this->valid = null;
$this->disabledOptions[] = $value;

if ($option = $this->getOption($value)) {
$option->getAttributes()->add('disabled', true);
}

if ($this->isSelectedOption($value)) {
$this->valid = false;
$this->addMessage(sprintf($this->translate("'%s' is not allowed here"), $value));
}

return $this;
}

Expand Down Expand Up @@ -116,6 +102,15 @@ public function setOptions(array $options)
return $this;
}

public function addDefaultValidators()
{
$this->getPossibleValues = Closure::fromCallable(function (): array {
return array_diff(array_keys($this->options), $this->disabledOptions);
});

$this->getValidators()->add(new DeferredInArrayValidator($this->getPossibleValues));
}

protected function makeOption($value, $label)
{
if (is_array($label)) {
Expand Down