Skip to content
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

[5.3] Fixed the validator bug in hydrateFiles when removing the files array #15663

Merged
merged 17 commits into from
Oct 5, 2016
Merged
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
16 changes: 12 additions & 4 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,22 @@ protected function hydrateFiles(array $data, $arrayKey = null)
}

foreach ($data as $key => $value) {
$key = ($arrayKey) ? "$arrayKey.$key" : $key;
$new_key = ($arrayKey) ? "$arrayKey.$key" : $key;

// If this value is an instance of the HttpFoundation File class we will
// remove it from the data array and add it to the files array, which
// we use to conveniently separate out these files from other data.
if ($value instanceof File) {
$this->files[$key] = $value;
$this->files[$new_key] = $value;

unset($data[$key]);
} elseif (is_array($value)) {
$this->hydrateFiles($value, $key);
if (! empty($value)) {
$value = $this->hydrateFiles($value, $new_key);
if (empty($value)) {
unset($data[$key]);
}
}
}
}

Expand Down Expand Up @@ -334,7 +339,10 @@ public function sometimes($attribute, $rules, callable $callback)
*/
public function each($attribute, $rules)
{
$data = Arr::dot($this->initializeAttributeOnData($attribute));
$data = array_merge(
Arr::dot($this->initializeAttributeOnData($attribute)),
$this->files
);

$pattern = str_replace('\*', '[^\.]+', preg_quote($attribute));

Expand Down
42 changes: 42 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,12 @@ public function testValidateRequired()
$file = new File(__FILE__, false);
$v = new Validator($trans, ['name' => $file], ['name' => 'Required']);
$this->assertTrue($v->passes());

$file = new File(__FILE__, false);
$foo = new File(__FILE__, false);
$v = new Validator($trans, ['name' => [$file, $foo]], ['name.0' => 'Required', 'name.1' => 'Required']);
$this->assertTrue($v->passes());
$this->assertEmpty($v->getData());
}

public function testValidateRequiredWith()
Expand Down Expand Up @@ -3199,6 +3205,42 @@ public function testValidMethod()
]);
}

public function testFilesHydration()
{
$trans = $this->getIlluminateArrayTranslator();
$file = new File(__FILE__, false);
$v = new Validator($trans, ['file' => $file, 'text' => 'text'], ['text' => 'Required']);
$this->assertEquals(['file' => $file], $v->getFiles());
$this->assertEquals(['text' => 'text'], $v->getData());
}

public function testArrayOfFilesHydration()
{
$trans = $this->getIlluminateArrayTranslator();
$file = new File(__FILE__, false);
$file2 = new File(__FILE__, false);
$v = new Validator($trans, ['file' => [$file, $file2], 'text' => 'text'], ['text' => 'Required']);
$this->assertEquals(['file.0' => $file, 'file.1' => $file2], $v->getFiles());
$this->assertEquals(['text' => 'text'], $v->getData());
}

public function testMultipleFileUploads()
{
$trans = $this->getIlluminateArrayTranslator();
$file = new File(__FILE__, false);
$file2 = new File(__FILE__, false);
$v = new Validator($trans, ['file' => [$file, $file2]], ['file.*' => 'Required|mimes:xls']);
$this->assertFalse($v->passes());
}

public function testFileUploads()
{
$trans = $this->getIlluminateArrayTranslator();
$file = new File(__FILE__, false);
$v = new Validator($trans, ['file' => $file], ['file' => 'Required|mimes:xls']);
$this->assertFalse($v->passes());
}

protected function getTranslator()
{
return m::mock('Symfony\Component\Translation\TranslatorInterface');
Expand Down