-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7933 from kenjis/feat-validation-callable
feat: [Validation] Callable Rules
- Loading branch information
Showing
6 changed files
with
196 additions
and
8 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,43 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use Config\Services; | ||
|
||
class Form extends BaseController | ||
{ | ||
// Define a custom validation rule. | ||
public function _ruleEven($value): bool | ||
{ | ||
return (int) $value % 2 === 0; | ||
} | ||
|
||
public function process() | ||
{ | ||
// ... | ||
|
||
$validation = Services::validation(); | ||
$validation->setRules( | ||
[ | ||
'foo' => [ | ||
'required', | ||
// Specify the method in this controller as a rule. | ||
[$this, '_ruleEven'], | ||
], | ||
], | ||
[ | ||
// Errors | ||
'foo' => [ | ||
// Specify the array key for the callable rule. | ||
1 => 'The value is not even.', | ||
], | ||
], | ||
); | ||
|
||
if (! $validation->run($data)) { | ||
// handle validation errors | ||
} | ||
|
||
// ... | ||
} | ||
} |
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,22 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use Config\Services; | ||
|
||
class Form extends BaseController | ||
{ | ||
// Define a custom validation rule. | ||
public function _ruleEven($value, $data, &$error, $field): bool | ||
{ | ||
if ((int) $value % 2 === 0) { | ||
return true; | ||
} | ||
|
||
$error = 'The value is not even.'; | ||
|
||
return false; | ||
} | ||
|
||
// ... | ||
} |