-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to nested validation rules
- Loading branch information
1 parent
485da0f
commit 9d19d1e
Showing
5 changed files
with
490 additions
and
18 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,55 @@ | ||
<?php | ||
|
||
namespace Illuminate\Validation; | ||
|
||
use Illuminate\Support\Arr; | ||
|
||
class NestedRules | ||
{ | ||
/** | ||
* The callback to execute. | ||
* | ||
* @var callable | ||
*/ | ||
protected $callback; | ||
|
||
/** | ||
* Create a new nested rule instance. | ||
* | ||
* @param callable $callback | ||
* @return void | ||
*/ | ||
public function __construct(callable $callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
/** | ||
* Compile the callback into an array of rules. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @param mixed $data | ||
* @return \stdClass | ||
*/ | ||
public function compile($attribute, $value, $data = null) | ||
{ | ||
$rules = call_user_func($this->callback, $attribute, $value, $data); | ||
|
||
$parser = new ValidationRuleParser( | ||
Arr::undot(Arr::wrap($data)) | ||
); | ||
|
||
if (is_array($rules) && Arr::isAssoc($rules)) { | ||
$nested = []; | ||
|
||
foreach ($rules as $key => $rule) { | ||
$nested[$attribute.'.'.$key] = $rule; | ||
} | ||
|
||
return $parser->explode($nested); | ||
} | ||
|
||
return $parser->explode([$attribute => $rules]); | ||
} | ||
} |
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
Oops, something went wrong.