-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[5.5] Support custom validation rule objects. (#19155)
* Support custom validation rule objects. This is a slightly different approach to use custom validation objects compared to the current Validator::extend approach. With this addition, you can define an object that implements Illuminate\Validation\ValidationRule and then include it in your validation rules like so: ‘name’ => [new ValidName]. This provides a quick way to define custom validation rules without needing to call Validator::extend at all. * Apply fixes from StyleCI (#19156) * Remove unused property. * support implicit validation rules * Apply fixes from StyleCI (#19158) * move contracts * Apply fixes from StyleCI (#19159)
- Loading branch information
1 parent
8adbaa7
commit 6704b2a
Showing
6 changed files
with
280 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Illuminate\Contracts\Validation; | ||
|
||
interface ImplicitRule extends Rule | ||
{ | ||
// | ||
} |
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 Illuminate\Contracts\Validation; | ||
|
||
interface Rule | ||
{ | ||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value); | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ | ||
public function message(); | ||
} |
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,70 @@ | ||
<?php | ||
|
||
namespace Illuminate\Validation; | ||
|
||
use Illuminate\Contracts\Validation\Rule as RuleContract; | ||
|
||
class ClosureValidationRule implements RuleContract | ||
{ | ||
/** | ||
* The callback that validates the attribute. | ||
* | ||
* @var \Closure | ||
*/ | ||
public $callback; | ||
|
||
/** | ||
* Indicates if the validation callback failed. | ||
* | ||
* @var bool | ||
*/ | ||
public $failed = false; | ||
|
||
/** | ||
* The validation error message. | ||
* | ||
* @var string|null | ||
*/ | ||
public $message; | ||
|
||
/** | ||
* Create a new Closure based validation rule. | ||
* | ||
* @param \Closure $callback | ||
* @return void | ||
*/ | ||
public function __construct($callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value) | ||
{ | ||
$this->failed = false; | ||
|
||
$this->callback->__invoke($attribute, $value, function ($message) { | ||
$this->failed = true; | ||
|
||
$this->message = $message; | ||
}); | ||
|
||
return ! $this->failed; | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ | ||
public function message() | ||
{ | ||
return $this->message; | ||
} | ||
} |
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