Skip to content

Commit

Permalink
Changed validationError to protected property from private to be mo…
Browse files Browse the repository at this point in the history
…re flexible
  • Loading branch information
jamielsharief committed Jul 31, 2020
1 parent 906486a commit df25ed2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ 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/spec/v2.0.0.html).


## [unreleased]

### Added

- Added `invalidate` to `ValidateTrait`

### Changed

- Changed `validationError` to protected property from private to be more flexible

## [1.3.0] - 2020-07-31

### Added
Expand Down
18 changes: 16 additions & 2 deletions src/ValidateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ trait ValidateTrait
/**
* @var array
*/
private $validationErrors = [];

protected $validationErrors = [];

/**
* If validation fails you can use this to work with errors
Expand Down Expand Up @@ -64,6 +63,21 @@ public function validate(string $field, $name, array $options = []): void
$this->validator()->add($field, $name, $options);
}

/**
* Invalidates a field
*
* @param string $field
* @param string $error
* @return void
*/
public function invalidate(string $field, string $error) : void
{
if (! isset($this->validationErrors[$field])) {
$this->validationErrors[$field] = [];
}
$this->validationErrors[$field][] = $error;
}

/**
* Validates this object
*
Expand Down
3 changes: 3 additions & 0 deletions tests/ValidateTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ public function testValidate()

$this->assertEmpty($popo->errors());
$this->assertEmpty($popo->errors('name'));

$popo->invalidate('foo', 'bar');
$this->assertEquals(['bar'], $popo->errors('foo'));
}
}

0 comments on commit df25ed2

Please sign in to comment.