FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Allow * in attribute list to call each. · JavaDevl/framework@d8da8eb · GitHub

Commit d8da8eb

Browse files
committed
Allow * in attribute list to call each.
1 parent 2028d82 commit d8da8eb

2 files changed

Lines changed: 21 additions & 22 deletions

File tree

‎src/Illuminate/Validation/Validator.php‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@ public function __construct(TranslatorInterface $translator, array $data, array
166166
$this->translator = $translator;
167167
$this->customMessages = $messages;
168168
$this->data = $this->parseData($data);
169-
$this->rules = $this->explodeRules($rules);
170169
$this->customAttributes = $customAttributes;
170+
171+
// Explode the rules first so that the implicit ->each calls are made...
172+
$rules = $this->explodeRules($rules);
173+
174+
$this->rules = array_merge((array) $this->rules, $rules);
171175
}
172176

173177
/**
@@ -209,8 +213,14 @@ protected function parseData(array $data, $arrayKey = null)
209213
*/
210214
protected function explodeRules($rules)
211215
{
212-
foreach ($rules as $key => &$rule) {
213-
$rule = (is_string($rule)) ? explode('|', $rule) : $rule;
216+
foreach ($rules as $key => $rule) {
217+
if (Str::contains($key, '*')) {
218+
$this->each($key, $rule);
219+
220+
unset($rules[$key]);
221+
} else {
222+
$rules[$key] = (is_string($rule)) ? explode('|', $rule) : $rule;
223+
}
214224
}
215225

216226
return $rules;
@@ -261,22 +271,14 @@ public function sometimes($attribute, $rules, callable $callback)
261271
*/
262272
public function each($attribute, $rules)
263273
{
264-
$data = Arr::get($this->data, $attribute);
274+
$data = Arr::dot($this->data);
265275

266-
if (! is_array($data)) {
267-
if ($this->hasRule($attribute, 'Array')) {
268-
return;
269-
}
270-
271-
throw new InvalidArgumentException('Attribute for each() must be an array.');
272-
}
273-
274-
foreach ($data as $dataKey => $dataValue) {
275-
foreach ((array) $rules as $ruleKey => $ruleValue) {
276-
if (! is_string($ruleKey)) {
277-
$this->mergeRules("$attribute.$dataKey", $ruleValue);
278-
} else {
279-
$this->mergeRules("$attribute.$dataKey.$ruleKey", $ruleValue);
276+
foreach ($data as $key => $value) {
277+
if (Str::startsWith($key, $attribute) || Str::is($attribute, $key)) {
278+
foreach ((array) $rules as $ruleKey => $ruleValue) {
279+
if (! is_string($ruleKey) || Str::endsWith($key, $ruleKey)) {
280+
$this->mergeRules($key, $ruleValue);
281+
}
280282
}
281283
}
282284
}
@@ -1655,7 +1657,7 @@ protected function getCustomMessageFromTranslator($customKey)
16551657
{
16561658
$shortKey = str_replace('validation.custom.', '', $customKey);
16571659

1658-
$customMessages = array_dot(
1660+
$customMessages = Arr::dot(
16591661
(array) $this->translator->trans('validation.custom')
16601662
);
16611663

‎tests/Validation/ValidationValidatorTest.php‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,9 +1788,6 @@ public function testValidateEachWithNonArrayWithArrayRule()
17881788
$this->assertFalse($v->passes());
17891789
}
17901790

1791-
/**
1792-
* @expectedException InvalidArgumentException
1793-
*/
17941791
public function testValidateEachWithNonArrayWithoutArrayRule()
17951792
{
17961793
$trans = $this->getRealTranslator();

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL