Skip to content

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

Merged
taylorotwell merged 17 commits into
laravel:5.3from
chengguoqiang:5.3
Oct 5, 2016
Merged

[5.3] Fixed the validator bug in hydrateFiles when removing the files array#15663
taylorotwell merged 17 commits into
laravel:5.3from
chengguoqiang:5.3

Conversation

@chengguoqiang

@chengguoqiang chengguoqiang commented Sep 29, 2016

Copy link
Copy Markdown
Contributor

fixed the bug of the method hydrateFiles when remove the files array.

@chengguoqiang

Copy link
Copy Markdown
Contributor Author

tests for #15660

@themsaid

themsaid commented Sep 29, 2016

Copy link
Copy Markdown
Member

The test you added is currently passing without the changes you are introducing, can you please describe what are you trying to fix?

@GrahamCampbell GrahamCampbell changed the title add tests [5.3] add tests Sep 29, 2016
@GrahamCampbell GrahamCampbell changed the title [5.3] add tests [5.3] Validator improvement Sep 29, 2016
@chengguoqiang

Copy link
Copy Markdown
Contributor Author

@themsaid Upload the files array , hydrateFiles can't remove it from data array

@themsaid

Copy link
Copy Markdown
Member

@chengguoqiang can you please add a failing test?

@taylorotwell

Copy link
Copy Markdown
Member

We also need a much better description of what this is attempting to solve / fix / do before merging.

@chengguoqiang

Copy link
Copy Markdown
Contributor Author

@themsaid I added a failing test, please check, thank you.

@chengguoqiang chengguoqiang changed the title [5.3] Validator improvement [5.3] Validator improvement - fixed the bug of the method hydrateFiles when remove the files array. Sep 29, 2016
@chengguoqiang

Copy link
Copy Markdown
Contributor Author

@taylorotwell the description was modified, would you please check the description for me

@themsaid

Copy link
Copy Markdown
Member

Well you just pointed out a bug, but the fix isn't actually complete. The validator works fine with this bug except for the part of separating data from files for array file uploads, means that now $this->data won't have these file uploads, and the each() method relys only on the data attribute, so you'll need to update the each() method and merge the files array to the $data array inside the method before starting the iteration.

To ensure everything is working, you may add these tests:

public function testFilesHydration()
{
    $trans = $this->getIlluminateArrayTranslator();
    $file = new File(__FILE__, false);
    $v = new Validator($trans, ['file' => $file, 'text' => 'text'], ['name' => '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'], ['name' => '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());
}

@chengguoqiang

Copy link
Copy Markdown
Contributor Author

@themsaid improvement

@themsaid

themsaid commented Sep 30, 2016

Copy link
Copy Markdown
Member

I'm sorry but your version of testMultipleFileUploads() doesn't really test anything, same for testFileUploads ().

These simple tests should pass:

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());
}

If you try it testMultipleFileUploads() will fail, which is the problem we need to solve, this test should pass.

@chengguoqiang

chengguoqiang commented Sep 30, 2016

Copy link
Copy Markdown
Contributor Author

I update the each() method and merge the files array to the $data array inside the method before starting the iteration. these simple tests what you given run fail:

Comment thread src/Illuminate/Validation/Validator.php Outdated
$value = $this->getValue($attribute);

if ($value != '__missing__') {
if (! is_null($value)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this change? null is a value we still want to get.

@themsaid

Copy link
Copy Markdown
Member

In the each() method you need to have this:

$data = array_merge(
            Arr::dot($this->initializeAttributeOnData($attribute)), 
            $this->files
        );

so that it looks into the files array as well, if you have this the tests will pass.

@chengguoqiang

Copy link
Copy Markdown
Contributor Author

the tests pass, thank you very match

@GrahamCampbell GrahamCampbell changed the title [5.3] Validator improvement - fixed the bug of the method hydrateFiles when remove the files array. [5.3] Fixed the validator bug in hydrateFiles when removing the files array Oct 3, 2016
@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Please rebase and squash to one commtit.

@taylorotwell taylorotwell merged commit 9900c9f into laravel:5.3 Oct 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants