Skip to content

[12.x] Introduce Rule::anyOf() for Validating Against Multiple Rule Sets (#54880) #54946

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/Translation/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
'alpha' => 'The :attribute field must only contain letters.',
'alpha_dash' => 'The :attribute field must only contain letters, numbers, dashes, and underscores.',
'alpha_num' => 'The :attribute field must only contain letters and numbers.',
'any_of' => 'The :attribute field is invalid.',
'array' => 'The :attribute field must be an array.',
'ascii' => 'The :attribute field must only contain single-byte alphanumeric characters and symbols.',
'before' => 'The :attribute field must be a date before :date.',
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Validation\Rules\AnyOf;
use Illuminate\Validation\Rules\ArrayRule;
use Illuminate\Validation\Rules\Can;
use Illuminate\Validation\Rules\Date;
Expand Down Expand Up @@ -246,6 +247,19 @@ public static function numeric()
return new Numeric;
}

/**
* Get an "any of" rule builder instance.
*
* @param array
* @return \Illuminate\Validation\Rules\AnyOf
*
* @throws \InvalidArgumentException
*/
public static function anyOf($rules)
{
return new AnyOf($rules);
}

/**
* Compile a set of rules for an attribute.
*
Expand Down
101 changes: 101 additions & 0 deletions src/Illuminate/Validation/Rules/AnyOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Illuminate\Validation\Rules;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Contracts\Validation\ValidatorAwareRule;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use InvalidArgumentException;

class AnyOf implements Rule, ValidatorAwareRule
{
/**
* The rules to match against.
*
* @var array
*/
protected array $rules = [];

/**
* The error message after validation, if any.
*
* @var array
*/
protected $messages = [];

/**
* The validator performing the validation.
*
* @var \Illuminate\Validation\Validator
*/
protected $validator;

/**
* Sets the validation rules to match against.
*
* @param Illuminate\Contracts\Validation\ValidationRule[][] $rules
*
* @throws \InvalidArgumentException
*/
public function __construct($rules)
{
if (! is_array($rules)) {
throw new InvalidArgumentException('The provided value must be an array of validation rules.');
}

$this->rules = $rules;
}

/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
$this->messages = [];

foreach ($this->rules as $rule) {
$validator = Validator::make(
Arr::isAssoc(Arr::wrap($value)) ? $value : [$attribute => $value],
Arr::isAssoc(Arr::wrap($rule)) ? $rule : [$attribute => $rule],
$this->validator->customMessages,
$this->validator->customAttributes
);

if ($validator->passes()) {
return true;
}
}

$this->validator->addFailure($attribute, 'any_of');

return false;
}

/**
* Get the validation error messages.
*
* @return array
*/
public function message()
{
return $this->messages;
}

/**
* Set the current validator.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return $this
*/
public function setValidator($validator)
{
$this->validator = $validator;

return $this;
}
}
Loading
Loading