diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index af65aaca83ff..c9960ffe3a0a 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -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]); + } + } } } @@ -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)); diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 13be518f587a..cff5c26c6b45 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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() @@ -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');