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

Don't cache element validation state #97

Merged
merged 1 commit into from
Dec 12, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/FormDecorator/DdDtDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getAttributes()
$attributes = parent::getAttributes();

// TODO: only when sent?!
if ($this->wrappedElement->hasBeenValidatedAndIsNotValid()) {
if ($this->wrappedElement->hasMessages()) {
$classes = $attributes->get('class');
if (
empty($classes)
Expand Down
2 changes: 1 addition & 1 deletion src/FormDecorator/DivDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected function assembleLabel()

protected function assemble()
{
if ($this->formElement->hasBeenValidated() && ! $this->formElement->isValid()) {
if ($this->formElement->hasMessages()) {
$this->getAttributes()->add('class', static::ERROR_HINT_CLASS);
}

Expand Down
34 changes: 23 additions & 11 deletions src/FormElement/BaseFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ abstract class BaseFormElement extends BaseHtmlElement implements FormElement, V
/** @var bool Whether the element is required */
protected $required = false;

/** @var null|bool Whether the element is valid; null if the element has not been validated yet, bool otherwise */
protected $valid;

/** @var ValidatorChain Registered validators */
protected $validators;

Expand All @@ -42,6 +39,13 @@ abstract class BaseFormElement extends BaseHtmlElement implements FormElement, V
/** @var array Value candidates of the element */
protected $valueCandidates = [];

/**
* @deprecated STOP: Do not use this property!!
*
* @var bool
*/
protected $valid;

/**
* Create a new form element
*
Expand Down Expand Up @@ -153,8 +157,14 @@ public function setRequired($required = true)

public function isValid()
{
if ($this->valid === null) {
$this->validate();
if ($this->valid !== null) {
return $this->valid;
}

$valid = $this->validate();
// validate() may return `$this` in child classes
if (is_bool($valid)) {
return $valid;
}

return $this->valid;
Expand All @@ -165,11 +175,11 @@ public function isValid()
*
* @return bool
*
* @deprecated Use {@link hasBeenValidated()} in combination with {@link isValid()} instead
* @deprecated Use {@see self::hasMessages()} instead
*/
public function hasBeenValidatedAndIsNotValid()
{
return $this->valid !== null && ! $this->valid;
return $this->hasBeenValidated() && ! $this->valid;
}

/**
Expand Down Expand Up @@ -238,7 +248,6 @@ public function setValue($value)
} else {
$this->value = $value;
}
$this->valid = null;

return $this;
}
Expand All @@ -262,16 +271,19 @@ public function onRegistered(Form $form)
/**
* Validate the element using all registered validators
*
* @return $this
* @return bool
*/
public function validate()
{
$this->valid = $this->getValidators()->isValid($this->getValue());
$valid = $this->getValidators()->isValid($this->getValue());
$this->addMessages($this->getValidators()->getMessages());

return $this;
return $valid;
}

/**
* @deprecated Do not use this method
*/
public function hasBeenValidated()
{
return $this->valid !== null;
Expand Down
8 changes: 3 additions & 5 deletions src/FormElement/SelectElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ public function validate()
|| $option->getAttributes()->has('disabled')
)
) {
$this->valid = false;
$this->addMessage("'$value' is not allowed here");
} elseif ($this->isRequired() && $value === null) {
$this->valid = false;
$this->addMessage('This field is required');
} else {
parent::validate();
return parent::validate();
}

return $this;
return false;
}

public function deselect()
Expand All @@ -68,7 +67,6 @@ public function disableOption($value)
$option->getAttributes()->add('disabled', true);
}
if ($this->getValue() == $value) {
$this->valid = false;
$this->addMessage("'$value' is not allowed here");
}

Expand Down