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

[5.4] Validator rules failing when using ConvertEmptyStringsToNull middleware #17558

Closed
JoshMountain opened this issue Jan 26, 2017 · 4 comments

Comments

@JoshMountain
Copy link

JoshMountain commented Jan 26, 2017

  • Laravel Version: 5.4
  • PHP Version: 7.1.0
  • Database Driver & Version: MySQL 5.5.5

Description:

When using the new ConvertEmptyStringsToNull middleware, validator rules will fail for empty fields.

Steps To Reproduce:

Take for example a two input form, asking for the user's name and website URL. The name is required but the URL is optional. When posted, the form validator may look like this:

<?php

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'website-url' => 'url',
    ]);

    dd('Valid data sent');
}

If you submit the form with only a name filled out and a blank website-url field, the validator will fail saying that the null value of $request->input('website-url') is not a valid URL. The validator should not be checking for a valid URL since the field is not required.

@JoshMountain JoshMountain changed the title [5.4] Validator methods failing when using ConvertEmptyStringsToNull middleware [5.4] Validator rules failing when using ConvertEmptyStringsToNull middleware Jan 26, 2017
@themsaid
Copy link
Member

If you use this middleware you say that you want to convert all empty strings to null, you'll need to prepare your rules for this change, use the nullable rule for all optional fields in this case.

@JoshMountain
Copy link
Author

JoshMountain commented Jan 31, 2017

@irsyadadl You need to add nullable to your rule set, like this:

<?php

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
        'website-url' => 'nullable|url',
    ]);

    dd('Valid data sent');
}

@jimbothegiant
Copy link

Swapping out 'sometimes' for 'nullable' fixed this for me when using the ConvertEmptyStringsToNull middleware

@juukie
Copy link
Contributor

juukie commented Apr 26, 2017

Couldn't find anything in upgrade guide. Glad I found this issue.
Adding nullable to the rules worked for me!

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

No branches or pull requests

4 participants