Skip to content

Commit

Permalink
Add validation getters
Browse files Browse the repository at this point in the history
  • Loading branch information
nie7321 committed Dec 6, 2023
1 parent 3dc930f commit a263425
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/).

## Unreleased
## [v0.13.0 - 2023-12-06]
### Added
- Added getters to the `ComponentInterface` for validation rules.

## [v0.12.2 - 2023-05-22]
### Fixed
- This fixes a bug introduced in v0.12.1; `show:false` in a `SimpleConditional` was being ignored instead of evaluated correctly.
Expand Down
7 changes: 6 additions & 1 deletion src/Components/BaseComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,13 @@ protected function postProcessValidationsForMultiple(string $fieldKey): MessageB
return $bag;
}

protected function validation(string $name): mixed
public function validation(string $name): mixed
{
return Arr::get($this->validations, $name);
}

public function validations(): array
{
return $this->validations;
}
}
10 changes: 10 additions & 0 deletions src/Components/ComponentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,14 @@ public function transformations(): array;
* Returns the default value for the component, if one is configured.
*/
public function defaultValue(): mixed;

/**
* Get a validation rule by name.
*/
public function validation(string $name): mixed;

/**
* Get all validations for the component.
*/
public function validations(): array;
}
4 changes: 4 additions & 0 deletions tests/Components/TestCases/BaseComponentTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function testCanValidate(): void
* @covers ::isCalculated
* @covers ::calculation
* @covers ::defaultValue
* @covers ::validation
* @covers ::validations
*/
public function testGetters(): void
{
Expand All @@ -58,6 +60,8 @@ public function testGetters(): void
$this->assertFalse($component->isCalculated());
$this->assertNull($component->calculation());
$this->assertEquals('foo', $component->defaultValue());
$this->assertNull($component->validation('required'));
$this->assertEmpty($component->validations());
}

/**
Expand Down

0 comments on commit a263425

Please sign in to comment.