Skip to content

Commit

Permalink
Make addDefaultValidators() protected and pass validator chain
Browse files Browse the repository at this point in the history
The chain is now passed to save calls to `getValidators()` and is now
protected as it is not intended to be called outside of the class.
  • Loading branch information
lippserd authored and nilmerg committed Nov 9, 2022
1 parent 312e36f commit c3293ce
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
19 changes: 10 additions & 9 deletions src/FormElement/BaseFormElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,14 @@ public function hasBeenValidatedAndIsNotValid()
public function getValidators()
{
if ($this->validators === null) {
$this->validators = new ValidatorChain();
$this->addDefaultValidators();
$chain = new ValidatorChain();
$this->addDefaultValidators($chain);
$this->validators = $chain;
}

return $this->validators;
}

/**
* Add default validators
*/
public function addDefaultValidators()
{
}

/**
* Set the validators
*
Expand Down Expand Up @@ -317,6 +311,13 @@ public function getValueAttribute()
return $this->getValue();
}

/**
* Add default validators
*/
protected function addDefaultValidators(ValidatorChain $chain): void
{
}

protected function registerValueCallback(Attributes $attributes)
{
$attributes->registerAttributeCallback(
Expand Down
5 changes: 3 additions & 2 deletions src/FormElement/ColorElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
namespace ipl\Html\FormElement;

use ipl\Validator\HexColorValidator;
use ipl\Validator\ValidatorChain;

class ColorElement extends InputElement
{
protected $type = 'color';

public function addDefaultValidators(): void
protected function addDefaultValidators(ValidatorChain $chain): void
{
$this->getValidators()->add(new HexColorValidator());
$chain->add(new HexColorValidator());
}
}
5 changes: 3 additions & 2 deletions src/FormElement/LocalDateTimeElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use ipl\Validator\DateTimeValidator;
use ipl\Validator\ValidatorChain;

class LocalDateTimeElement extends InputElement
{
Expand Down Expand Up @@ -45,8 +46,8 @@ public function getValueAttribute()
return $this->value->format(static::FORMAT);
}

public function addDefaultValidators()
protected function addDefaultValidators(ValidatorChain $chain): void
{
$this->getValidators()->add(new DateTimeValidator());
$chain->add(new DateTimeValidator());
}
}

0 comments on commit c3293ce

Please sign in to comment.