1. Go to this page and download the library: Download bradietilley/laravel-rules library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use BradieTilley\Rules\Rule;
return [
'my_field' => Rule::make()->
[
'my_field' => [
'
Rule::make()
/**
* You can use the methods provided
*/
->vel
*/
->rule('min:2')
/**
* You can pass in another Rule object which will be merged in
*/
->rule(Rule::make()->max(255))
/**
* You can pass in a `\Illuminate\Contracts\Validation\Rule` object
*
* Note: This Laravel interface is deprecated and will be dropped in future versions of Laravel. It is recommended to not use this interface.
*/
->rule(new RuleThatImplementsRule())
/**
* You can pass in a `\Illuminate\Contracts\Validation\InvokableRule` object
*
* Note: This Laravel interface is deprecated and will be dropped in future versions of Laravel. It is recommended to not use this interface.
*/
->rule(new RuleThatImplementsInvokableRule())
/**
* You can pass in a `\Illuminate\Contracts\Validation\ValidationRule` object
*/
->rule(new RuleThatImplementsValidationRule())
/**
* You can pass in any array of rules. The array values can be any of the
* above rule types: strings, Rule objects, ValidationRule instances, etc
*/
->rule([
Rule::make()->rule([
'min:1',
]),
'max:25',
new Unique('table', 'column'),
]);
/**
* Example using a closure
*/
public function rules(): array
{
$integerRule = function (Rule $rule) {
$rule->integer()->max(100);
}
return [
'percent' => Rule::make()
->with($integerRule),
];
}
/**
* Example using a first class callable
*/
function integerRule(Rule $rule)
{
$rule->integer()->max(100);
}
public function rules(): array
{
return [
'percent' => Rule::make()
->with(integerRule(...)),
];
}
/**
* Example using a callable invokable class
*/
class IntegerRule
{
public function __invoke(Rule $rule)
{
$rule->integer()->max(100);
}
}
public function rules(): array
{
return [
'percent' => Rule::make()
->with(new IntegerRule()),
];
}
/**
* The above examples would all return:
*/
[
'percent' => [
'integer',
'max:100',
],
]
\BradieTilley\Rules\Rule::using(\App\Rules\CustomRule::class);
// via ::make()
\BradieTilley\Rules\Rule::make(); // instanceof App\Rules\CustomRule
// via the helper function
rule(); // instanceof App\Rules\CustomRule
public function email(string ...$flags): static
{
return parent::email(...$flags)->min(5)->max(255);
}
CustomRule::make()-> '',
'max:255',
],
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($this->someCondition) {
return; // pass
}
if ($this->otherCondition) {
$fail('Some error message');
return; // you can't return anything so you can't join the $fail and return lines together
}
}
public function run(string $attribute, mixed $value): static
{
if ($this->someCondition) {
return $this->pass();
}
if ($this->otherCondition) {
return $this->fail('Some error message');
}
return $this->pass();
}