Skip to content

[5.3] Fix sending empty input fields as null inputs when running tests #15691

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

Merged
merged 1 commit into from
Oct 3, 2016
Merged

[5.3] Fix sending empty input fields as null inputs when running tests #15691

merged 1 commit into from
Oct 3, 2016

Conversation

uxweb
Copy link
Contributor

@uxweb uxweb commented Sep 30, 2016

Added a filter to get only the file inputs called with this->attach(input, value)

Note: This only happens when running tests.

If you are submitting a form that has input file fields and don't attach any file on those inputs
it will send those as normal inputs with a value of null causing a validation exception if any of
those fields have a validation rule of any kind (except required, that will be obvious).

I think there is no use in sending empty file fields as normal inputs with a value of null.

This happens because on how validation of null fields are handled now in 5.3.

If you don't expect those fields to be nullable, you'll have to add the 'nullable' validation rule to
make your test pass.

This filter prevents sending empty input file fields as normal inputs with a null value.

File inputs in an html form...

// within an html form in a view
<input type="file" name="front_photo">
<input type="file" name="back_photo">
<input type="file" name="left_photo">
<input type="file" name="right_photo">
<input type="submit" value="submit">

Test submitting the form...

// within a test class method...
$this->attach('path-to-file.ext', 'front_photo');

$this->press('submit');

Inspecting the input within the controller with dd($request->all())...

// without the filter i get:
[
    back_photo => null
    left_photo => null
    right_photo => null
]

// with the filter i get:
[]

…nput, value)

Note: This only happens when running tests.

If you are submitting a form that has input file fields and don't attach any file on those inputs
it will send those as normal inputs with a value of null causing a validation exception if any of
those fields have a validation rule of any kind (except required).

This happens because on how validation of null fields are handled now in 5.3.

If you don't expect those fields to be nullable, you'll have to add the 'nullable' validation rule to
make your test pass.

I think there is no use in sending empty file fields as normal inputs with a value of null.

This filter prevents sending empty input file fields as normal inputs with a null value.

File inputs in an html form...
```
    // within an html form in a view
    <input type="file" name="front_photo">
    <input type="file" name="back_photo">
    <input type="file" name="left_photo">
    <input type="file" name="right_photo">
    <input type="submit" value="submit">
```

Test submitting the form...
```
    // within a test class method...
    $this->attach('path-to-file.ext', 'front_photo');

    $this->press('submit');
```

Inspecting the input whitin the controller...
```
    // without the filter i get:
    [
        back_photo => null
        left_photo => null
        right_photo => null
    ]

    // with the filter i get:
    [
    ]
```
@uxweb uxweb changed the title [5.3] - Fix sending empty input fields as null inputs when running tests [5.3] Fix sending empty input fields as null inputs when running tests Sep 30, 2016
@taylorotwell taylorotwell merged commit 4a876ac into laravel:5.3 Oct 3, 2016
@themsaid
Copy link
Member

themsaid commented Oct 3, 2016

This change had to be reverted (543f161) since the issue is already fixed in #15574

The filterFiles() method in MakesHttpRequests will remove all empty files and send only fields with attached files to the request creator.

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.

3 participants