Replies: 1 comment
-
Here's how I do it on my project <?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule as RuleContract;
abstract class BaseInvokableValidationRule implements RuleContract
{
/**
* Indicates if the validation callback failed.
*
* @var bool
*/
public $failed = false;
/**
* The validation error message.
*
* @var string|null
*/
public $message;
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->failed = false;
$this->__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;
}
/**
* Run the validation
*
* @param string $attribute
* @param mixed $value
* @param callback $fail
* @return void
*/
abstract public function __invoke($attribute, $value, $fail);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I propose a new additional syntax for class based validation to use the same format as using closures:
Usage:
Beta Was this translation helpful? Give feedback.
All reactions