-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add requires rule to force another field to be present
- Loading branch information
1 parent
f5b1fcb
commit 66acb90
Showing
6 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Somnambulist\Components\Validation\Rules; | ||
|
||
class Requires extends Required | ||
{ | ||
protected bool $implicit = true; | ||
protected string $message = 'rule.requires'; | ||
|
||
public function fillParameters(array $params): self | ||
{ | ||
$this->params['fields'] = $params; | ||
|
||
return $this; | ||
} | ||
|
||
public function check(mixed $value): bool | ||
{ | ||
$this->assertHasRequiredParameters(['fields']); | ||
|
||
$fields = $this->parameter('fields'); | ||
|
||
foreach ($fields as $field) { | ||
if (!$this->validation->input()->has($field) || empty($this->validation->input()->get($field))) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters