diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index db37ba8372bb..998c3efe7cd7 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -65,13 +65,6 @@ class Validator implements ValidatorContract */ protected $data; - /** - * The files under validation. - * - * @var array - */ - protected $files = []; - /** * The initial rules provided. * @@ -204,7 +197,7 @@ public function __construct(TranslatorInterface $translator, array $data, array $this->translator = $translator; $this->customMessages = $messages; $this->customAttributes = $customAttributes; - $this->data = $this->hydrateFiles($this->parseData($data)); + $this->data = $this->parseData($data); $this->setRules($rules); } @@ -234,38 +227,6 @@ public function parseData(array $data) return $newData; } - /** - * Hydrate the files array. - * - * @param array $data - * @param string $arrayKey - * @return array - */ - protected function hydrateFiles(array $data, $arrayKey = null) - { - if (is_null($arrayKey)) { - $this->files = []; - } - - foreach ($data as $key => $value) { - $newKey = $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[$newKey] = $value; - - unset($data[$key]); - } elseif (is_array($value) && ! empty($value) && - empty($this->hydrateFiles($value, $newKey))) { - unset($data[$key]); - } - } - - return $data; - } - /** * Explode the rules into an array of rules. * @@ -342,9 +303,7 @@ public function sometimes($attribute, $rules, callable $callback) */ public function each($attribute, $rules) { - $data = array_merge( - Arr::dot($this->initializeAttributeOnData($attribute)), $this->files - ); + $data = Arr::dot($this->initializeAttributeOnData($attribute)); $pattern = str_replace('\*', '[^\.]+', preg_quote($attribute)); @@ -615,11 +574,7 @@ protected function attributesThatHaveMessages() */ protected function getValue($attribute) { - if (! is_null($value = Arr::get($this->data, $attribute))) { - return $value; - } elseif (! is_null($value = Arr::get($this->files, $attribute))) { - return $value; - } + return Arr::get($this->data, $attribute); } /** @@ -665,8 +620,7 @@ protected function passesOptionalCheck($attribute) { if ($this->hasRule($attribute, ['Sometimes'])) { return array_key_exists($attribute, Arr::dot($this->data)) - || in_array($attribute, array_keys($this->data)) - || array_key_exists($attribute, $this->files); + || in_array($attribute, array_keys($this->data)); } return true; @@ -838,7 +792,7 @@ protected function validateRequired($attribute, $value) */ protected function validatePresent($attribute, $value) { - return Arr::has(array_merge($this->data, $this->files), $attribute); + return Arr::has($this->data, $attribute); } /** @@ -850,7 +804,7 @@ protected function validatePresent($attribute, $value) */ protected function validateFilled($attribute, $value) { - if (Arr::has(array_merge($this->data, $this->files), $attribute)) { + if (Arr::has($this->data, $attribute)) { return $this->validateRequired($attribute, $value); } @@ -1026,7 +980,7 @@ protected function getPresentCount($attributes) $count = 0; foreach ($attributes as $key) { - if (Arr::get($this->data, $key) || Arr::get($this->files, $key)) { + if (Arr::get($this->data, $key)) { $count++; } } @@ -2192,7 +2146,7 @@ protected function getAttributeType($attribute) return 'numeric'; } elseif ($this->hasRule($attribute, ['Array'])) { return 'array'; - } elseif (array_key_exists($attribute, $this->files)) { + } elseif ($this->getValue($attribute) instanceof UploadedFile) { return 'file'; } @@ -2662,7 +2616,7 @@ protected function replaceAfter($message, $attribute, $rule, $parameters) */ public function attributes() { - return array_merge($this->data, $this->files); + return $this->data; } /** @@ -3098,29 +3052,6 @@ public function setValueNames(array $values) return $this; } - /** - * Get the files under validation. - * - * @return array - */ - public function getFiles() - { - return $this->files; - } - - /** - * Set the files under validation. - * - * @param array $files - * @return $this - */ - public function setFiles(array $files) - { - $this->files = $files; - - return $this; - } - /** * Get the Presence Verifier implementation. * diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index 9de505c418b9..ffbcae03b6c0 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -561,10 +561,12 @@ public function testValidateRequired() $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']); + $file2 = new File(__FILE__, false); + $v = new Validator($trans, ['files' => [$file, $file2]], ['files.0' => 'Required', 'files.1' => 'Required']); + $this->assertTrue($v->passes()); + + $v = new Validator($trans, ['files' => [$file, $file2]], ['files' => 'Required']); $this->assertTrue($v->passes()); - $this->assertEmpty($v->getData()); } public function testValidateRequiredWith() @@ -807,16 +809,14 @@ public function testFailedFileUploads() $file = m::mock('Symfony\Component\HttpFoundation\File\UploadedFile'); $file->shouldReceive('isValid')->andReturn(false); $file->shouldNotReceive('getSize'); - $v = new Validator($trans, [], ['photo' => 'Max:10']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']); $this->assertTrue($v->fails()); $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo')); // Even "required" will not run if the file failed to upload. $file = m::mock('Symfony\Component\HttpFoundation\File\UploadedFile'); $file->shouldReceive('isValid')->once()->andReturn(false); - $v = new Validator($trans, [], ['photo' => 'required']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'required']); $this->assertTrue($v->fails()); $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo')); @@ -824,16 +824,14 @@ public function testFailedFileUploads() // a file. Otherwise it should fail with the regular rule. $file = m::mock('Symfony\Component\HttpFoundation\File\UploadedFile'); $file->shouldReceive('isValid')->andReturn(false); - $v = new Validator($trans, [], ['photo' => 'string']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'string']); $this->assertTrue($v->fails()); $this->assertEquals(['validation.string'], $v->errors()->get('photo')); // Validation shouldn't continue if a file failed to upload. $file = m::mock('Symfony\Component\HttpFoundation\File\UploadedFile'); $file->shouldReceive('isValid')->once()->andReturn(false); - $v = new Validator($trans, [], ['photo' => 'file|mimes:pdf|min:10']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'file|mimes:pdf|min:10']); $this->assertTrue($v->fails()); $this->assertEquals(['validation.uploaded'], $v->errors()->get('photo')); } @@ -1149,14 +1147,12 @@ public function testValidateSize() $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock(); $file->expects($this->any())->method('getSize')->will($this->returnValue(3072)); - $v = new Validator($trans, [], ['photo' => 'Size:3']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Size:3']); $this->assertTrue($v->passes()); $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock(); $file->expects($this->any())->method('getSize')->will($this->returnValue(4072)); - $v = new Validator($trans, [], ['photo' => 'Size:3']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Size:3']); $this->assertFalse($v->passes()); } @@ -1189,14 +1185,12 @@ public function testValidateBetween() $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock(); $file->expects($this->any())->method('getSize')->will($this->returnValue(3072)); - $v = new Validator($trans, [], ['photo' => 'Between:1,5']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Between:1,5']); $this->assertTrue($v->passes()); $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock(); $file->expects($this->any())->method('getSize')->will($this->returnValue(4072)); - $v = new Validator($trans, [], ['photo' => 'Between:1,2']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Between:1,2']); $this->assertFalse($v->passes()); } @@ -1223,14 +1217,12 @@ public function testValidateMin() $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock(); $file->expects($this->any())->method('getSize')->will($this->returnValue(3072)); - $v = new Validator($trans, [], ['photo' => 'Min:2']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Min:2']); $this->assertTrue($v->passes()); $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock(); $file->expects($this->any())->method('getSize')->will($this->returnValue(4072)); - $v = new Validator($trans, [], ['photo' => 'Min:10']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Min:10']); $this->assertFalse($v->passes()); } @@ -1258,21 +1250,18 @@ public function testValidateMax() $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['isValid', 'getSize'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock(); $file->expects($this->any())->method('isValid')->will($this->returnValue(true)); $file->expects($this->at(1))->method('getSize')->will($this->returnValue(3072)); - $v = new Validator($trans, [], ['photo' => 'Max:10']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']); $this->assertTrue($v->passes()); $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['isValid', 'getSize'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock(); $file->expects($this->at(0))->method('isValid')->will($this->returnValue(true)); $file->expects($this->at(1))->method('getSize')->will($this->returnValue(4072)); - $v = new Validator($trans, [], ['photo' => 'Max:2']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:2']); $this->assertFalse($v->passes()); $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['isValid'])->setConstructorArgs([__FILE__, basename(__FILE__)])->getMock(); $file->expects($this->any())->method('isValid')->will($this->returnValue(false)); - $v = new Validator($trans, [], ['photo' => 'Max:10']); - $v->setFiles(['photo' => $file]); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:10']); $this->assertFalse($v->passes()); } @@ -1290,10 +1279,10 @@ public function testProperMessagesAreReturnedForSizes() $v->messages()->setFormat(':message'); $this->assertEquals('string', $v->messages()->first('name')); - $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\File')->setMethods(['getSize'])->setConstructorArgs([__FILE__, false])->getMock(); + $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['getSize', 'isValid'])->setConstructorArgs([__FILE__, false])->getMock(); $file->expects($this->any())->method('getSize')->will($this->returnValue(4072)); - $v = new Validator($trans, [], ['photo' => 'Max:3']); - $v->setFiles(['photo' => $file]); + $file->expects($this->any())->method('isValid')->will($this->returnValue(true)); + $v = new Validator($trans, ['photo' => $file], ['photo' => 'Max:3']); $this->assertFalse($v->passes()); $v->messages()->setFormat(':message'); $this->assertEquals('file', $v->messages()->first('photo')); @@ -1833,34 +1822,32 @@ public function testValidateImage() $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); $file->expects($this->any())->method('guessExtension')->will($this->returnValue('php')); - $v = new Validator($trans, [], ['x' => 'Image']); - $v->setFiles(['x' => $file]); + $v = new Validator($trans, ['x' => $file], ['x' => 'Image']); $this->assertFalse($v->passes()); - $v = new Validator($trans, [], ['x' => 'Image']); $file2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); $file2->expects($this->any())->method('guessExtension')->will($this->returnValue('jpeg')); - $v->setFiles(['x' => $file2]); + $v = new Validator($trans, ['x' => $file2], ['x' => 'Image']); $this->assertTrue($v->passes()); $file3 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); $file3->expects($this->any())->method('guessExtension')->will($this->returnValue('gif')); - $v->setFiles(['x' => $file3]); + $v = new Validator($trans, ['x' => $file3], ['x' => 'Image']); $this->assertTrue($v->passes()); $file4 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); $file4->expects($this->any())->method('guessExtension')->will($this->returnValue('bmp')); - $v->setFiles(['x' => $file4]); + $v = new Validator($trans, ['x' => $file4], ['x' => 'Image']); $this->assertTrue($v->passes()); $file5 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); $file5->expects($this->any())->method('guessExtension')->will($this->returnValue('png')); - $v->setFiles(['x' => $file5]); + $v = new Validator($trans, ['x' => $file5], ['x' => 'Image']); $this->assertTrue($v->passes()); $file6 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); $file6->expects($this->any())->method('guessExtension')->will($this->returnValue('svg')); - $v->setFiles(['x' => $file6]); + $v = new Validator($trans, ['x' => $file6], ['x' => 'Image']); $this->assertTrue($v->passes()); } @@ -1873,60 +1860,46 @@ public function testValidateImageDimensions() $v = new Validator($trans, ['x' => 'file'], ['x' => 'dimensions']); $this->assertTrue($v->fails()); - $v = new Validator($trans, [], ['x' => 'dimensions:min_width=1']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=1']); $this->assertTrue($v->passes()); - $v = new Validator($trans, [], ['x' => 'dimensions:min_width=5']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_width=5']); $this->assertTrue($v->fails()); - $v = new Validator($trans, [], ['x' => 'dimensions:max_width=10']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_width=10']); $this->assertTrue($v->passes()); - $v = new Validator($trans, [], ['x' => 'dimensions:max_width=1']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_width=1']); $this->assertTrue($v->fails()); - $v = new Validator($trans, [], ['x' => 'dimensions:min_height=1']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=1']); $this->assertTrue($v->passes()); - $v = new Validator($trans, [], ['x' => 'dimensions:min_height=5']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=5']); $this->assertTrue($v->fails()); - $v = new Validator($trans, [], ['x' => 'dimensions:max_height=10']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_height=10']); $this->assertTrue($v->passes()); - $v = new Validator($trans, [], ['x' => 'dimensions:max_height=1']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:max_height=1']); $this->assertTrue($v->fails()); - $v = new Validator($trans, [], ['x' => 'dimensions:width=3']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:width=3']); $this->assertTrue($v->passes()); - $v = new Validator($trans, [], ['x' => 'dimensions:height=2']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:height=2']); $this->assertTrue($v->passes()); - $v = new Validator($trans, [], ['x' => 'dimensions:min_height=2,ratio=3/2']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:min_height=2,ratio=3/2']); $this->assertTrue($v->passes()); - $v = new Validator($trans, [], ['x' => 'dimensions:ratio=1.5']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1.5']); $this->assertTrue($v->passes()); - $v = new Validator($trans, [], ['x' => 'dimensions:ratio=1/1']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1/1']); $this->assertTrue($v->fails()); - $v = new Validator($trans, [], ['x' => 'dimensions:ratio=1']); - $v->setFiles(['x' => $uploadedFile]); + $v = new Validator($trans, ['x' => $uploadedFile], ['x' => 'dimensions:ratio=1']); $this->assertTrue($v->fails()); } @@ -1940,8 +1913,7 @@ public function testValidateMimetypes() $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); $file->expects($this->any())->method('guessExtension')->will($this->returnValue('php')); - $v = new Validator($trans, [], ['x' => 'mimetypes:text/x-php']); - $v->setFiles(['x' => $file]); + $v = new Validator($trans, ['x' => $file], ['x' => 'mimetypes:text/x-php']); $this->assertTrue($v->passes()); } @@ -1952,15 +1924,13 @@ public function testValidateMime() $file = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension'])->setConstructorArgs($uploadedFile)->getMock(); $file->expects($this->any())->method('guessExtension')->will($this->returnValue('php')); - $v = new Validator($trans, [], ['x' => 'mimes:php']); - $v->setFiles(['x' => $file]); + $v = new Validator($trans, ['x' => $file], ['x' => 'mimes:php']); $this->assertTrue($v->passes()); $file2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\File\UploadedFile')->setMethods(['guessExtension', 'isValid'])->setConstructorArgs($uploadedFile)->getMock(); $file2->expects($this->any())->method('guessExtension')->will($this->returnValue('php')); $file2->expects($this->any())->method('isValid')->will($this->returnValue(false)); - $v = new Validator($trans, [], ['x' => 'mimes:php']); - $v->setFiles(['x' => $file2]); + $v = new Validator($trans, ['x' => $file2], ['x' => 'mimes:php']); $this->assertFalse($v->passes()); } @@ -1975,8 +1945,7 @@ public function testValidateFile() $v = new Validator($trans, ['x' => '1'], ['x' => 'file']); $this->assertTrue($v->fails()); - $v = new Validator($trans, [], ['x' => 'file']); - $v->setFiles(['x' => $file]); + $v = new Validator($trans, ['x' => $file], ['x' => 'file']); $this->assertTrue($v->passes()); } @@ -3221,25 +3190,6 @@ 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();