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

Change implementation 'default' rule, and support multiple file upload #71

Merged
merged 2 commits into from
Nov 26, 2018

Conversation

emsifa
Copy link
Member

@emsifa emsifa commented Nov 26, 2018

  • [out of context] Added Rakit\Validation\Rules\Interfaces\ModifyValue to modify attribute value while validating it's attribute.
  • [out of context] Default rule implements Rakit\Validation\Rules\Interfaces\ModifyValue to modify it's atttribute value.
  • Added Rakit\Validation\Rules\Interfaces\BeforeValidate so Rakit\Validation\Rule classes are able to do some preparation before validation running.
  • Rakit\Validation\Rules\UploadedFile class implements Rakit\Validation\Rules\Interfaces\BeforeValidate to resolve multiple file upload values before validation running.

Here are the examples (embedded from ValidatorTest.php):

  • Indexed multiple file upload:
    public function testValidationShouldCorrectlyResolveMultipleFileUploads()
    {
    // Test from input files:
    // <input type="file" name="photos[]"/>
    // <input type="file" name="photos[]"/>
    $sampleInputFiles = [
    'photos' => [
    'name' => [
    'a.png',
    'b.jpeg',
    ],
    'type' => [
    'image/png',
    'image/jpeg',
    ],
    'size' => [
    1000,
    2000,
    ],
    'tmp_name' => [
    __DIR__.'/a.png',
    __DIR__.'/b.jpeg',
    ],
    'error' => [
    UPLOAD_ERR_OK,
    UPLOAD_ERR_OK,
    ]
    ]
    ];
    $uploadedFileRule = $this->getMockedUploadedFileRule()->fileTypes('jpeg');
    $validation = $this->validator->validate($sampleInputFiles, [
    'photos.*' => ['required', $uploadedFileRule]
    ]);
    $this->assertFalse($validation->passes());
    $this->assertEquals($validation->getValidData(), [
    'photos' => [
    1 => [
    'name' => 'b.jpeg',
    'type' => 'image/jpeg',
    'size' => 2000,
    'tmp_name' => __DIR__.'/b.jpeg',
    'error' => UPLOAD_ERR_OK,
    ]
    ]
    ]);
    $this->assertEquals($validation->getInvalidData(), [
    'photos' => [
    0 => [
    'name' => 'a.png',
    'type' => 'image/png',
    'size' => 1000,
    'tmp_name' => __DIR__.'/a.png',
    'error' => UPLOAD_ERR_OK,
    ]
    ]
    ]);
    }
  • Key based multiple file upload:
    public function testValidationShouldCorrectlyResolveAssocFileUploads()
    {
    // Test from input files:
    // <input type="file" name="photos[foo]"/>
    // <input type="file" name="photos[bar]"/>
    $sampleInputFiles = [
    'photos' => [
    'name' => [
    'foo' => 'a.png',
    'bar' => 'b.jpeg',
    ],
    'type' => [
    'foo' => 'image/png',
    'bar' => 'image/jpeg',
    ],
    'size' => [
    'foo' => 1000,
    'bar' => 2000,
    ],
    'tmp_name' => [
    'foo' => __DIR__.'/a.png',
    'bar' => __DIR__.'/b.jpeg',
    ],
    'error' => [
    'foo' => UPLOAD_ERR_OK,
    'bar' => UPLOAD_ERR_OK,
    ]
    ]
    ];
    $uploadedFileRule = $this->getMockedUploadedFileRule()->fileTypes('jpeg');
    $validation = $this->validator->validate($sampleInputFiles, [
    'photos.foo' => ['required', clone $uploadedFileRule],
    'photos.bar' => ['required', clone $uploadedFileRule],
    ]);
    $this->assertFalse($validation->passes());
    $this->assertEquals($validation->getValidData(), [
    'photos' => [
    'bar' => [
    'name' => 'b.jpeg',
    'type' => 'image/jpeg',
    'size' => 2000,
    'tmp_name' => __DIR__.'/b.jpeg',
    'error' => UPLOAD_ERR_OK,
    ]
    ]
    ]);
    $this->assertEquals($validation->getInvalidData(), [
    'photos' => [
    'foo' => [
    'name' => 'a.png',
    'type' => 'image/png',
    'size' => 1000,
    'tmp_name' => __DIR__.'/a.png',
    'error' => UPLOAD_ERR_OK,
    ]
    ]
    ]);
    }
  • Combined:
    public function testValidationShouldCorrectlyResolveComplexFileUploads()
    {
    // Test from input files:
    // <input type="file" name="files[foo][bar][baz]"/>
    // <input type="file" name="files[foo][bar][qux]"/>
    // <input type="file" name="files[photos][]"/>
    // <input type="file" name="files[photos][]"/>
    $sampleInputFiles = [
    'files' => [
    'name' => [
    'foo' => [
    'bar' => [
    'baz' => 'foo-bar-baz.jpeg',
    'qux' => 'foo-bar-qux.png',
    ]
    ],
    'photos' => [
    'photos-0.png',
    'photos-1.jpeg',
    ]
    ],
    'type' => [
    'foo' => [
    'bar' => [
    'baz' => 'image/jpeg',
    'qux' => 'image/png',
    ]
    ],
    'photos' => [
    'image/png',
    'image/jpeg',
    ]
    ],
    'size' => [
    'foo' => [
    'bar' => [
    'baz' => 500,
    'qux' => 750,
    ]
    ],
    'photos' => [
    1000,
    2000,
    ]
    ],
    'tmp_name' => [
    'foo' => [
    'bar' => [
    'baz' => __DIR__.'/foo-bar-baz.jpeg',
    'qux' => __DIR__.'/foo-bar-qux.png',
    ]
    ],
    'photos' => [
    __DIR__.'/photos-0.png',
    __DIR__.'/photos-1.jpeg',
    ]
    ],
    'error' => [
    'foo' => [
    'bar' => [
    'baz' => UPLOAD_ERR_OK,
    'qux' => UPLOAD_ERR_OK,
    ]
    ],
    'photos' => [
    UPLOAD_ERR_OK,
    UPLOAD_ERR_OK,
    ]
    ]
    ]
    ];
    $uploadedFileRule = $this->getMockedUploadedFileRule()->fileTypes('jpeg');
    $validation = $this->validator->validate($sampleInputFiles, [
    'files.foo.bar.baz' => ['required', clone $uploadedFileRule],
    'files.foo.bar.qux' => ['required', clone $uploadedFileRule],
    'files.photos.*' => ['required', clone $uploadedFileRule],
    ]);
    $this->assertFalse($validation->passes());
    $this->assertEquals($validation->getValidData(), [
    'files' => [
    'foo' => [
    'bar' => [
    'baz' => [
    'name' => 'foo-bar-baz.jpeg',
    'type' => 'image/jpeg',
    'size' => 500,
    'tmp_name' => __DIR__.'/foo-bar-baz.jpeg',
    'error' => UPLOAD_ERR_OK,
    ]
    ]
    ],
    'photos' => [
    1 => [
    'name' => 'photos-1.jpeg',
    'type' => 'image/jpeg',
    'size' => 2000,
    'tmp_name' => __DIR__.'/photos-1.jpeg',
    'error' => UPLOAD_ERR_OK,
    ]
    ]
    ]
    ]);
    $this->assertEquals($validation->getInvalidData(), [
    'files' => [
    'foo' => [
    'bar' => [
    'qux' => [
    'name' => 'foo-bar-qux.png',
    'type' => 'image/png',
    'size' => 750,
    'tmp_name' => __DIR__.'/foo-bar-qux.png',
    'error' => UPLOAD_ERR_OK,
    ]
    ]
    ],
    'photos' => [
    0 => [
    'name' => 'photos-0.png',
    'type' => 'image/png',
    'size' => 1000,
    'tmp_name' => __DIR__.'/photos-0.png',
    'error' => UPLOAD_ERR_OK,
    ],
    ]
    ]
    ]);
    }

@emsifa emsifa merged commit 5ea3c11 into v1 Nov 26, 2018
@emsifa emsifa deleted the support-multiple-file-upload branch November 26, 2018 10:53
This was referenced Nov 26, 2018
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.

None yet

1 participant