Skip to content

Commit dc05717

Browse files
committed
Add Validation::hasError method
1 parent ee7d1ef commit dc05717

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/Validation.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,16 @@ public function setError(string $field, string $rule, array $args = []) : static
376376
return $this;
377377
}
378378

379+
/**
380+
* @param string $field
381+
*
382+
* @return bool
383+
*/
384+
public function hasError(string $field) : bool
385+
{
386+
return isset($this->errors[$field]);
387+
}
388+
379389
/**
380390
* Set a custom error message for a field rule.
381391
*

tests/ValidationTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ public function testSetError() : void
381381
);
382382
}
383383

384+
public function testHasError() : void
385+
{
386+
self::assertSame([], $this->validation->getErrors());
387+
self::assertFalse($this->validation->hasError('foo'));
388+
$this->validation->setError('foo', 'required');
389+
self::assertTrue($this->validation->hasError('foo'));
390+
}
391+
384392
public function testReplaceArgs() : void
385393
{
386394
$args = ['foo', 'bar', '{id}', '{x}', 'baz', '{email}'];

0 commit comments

Comments
 (0)