35

In Laravel 5.7 I am using form request validation:

public function rules() 
{
    return [
        'age' => 'integer',
        'title' => 'string|max:50'
    ];
}

If I submit a request to my API with this payload:

{
  "age": 24,
  "title": ""
}

Laravel returns the error:

{
    "message": "The given data was invalid.",
    "errors": {
        "title": [
            "The title must be a string."
        ]
    }
}

I would expect it to pass the validation, since the title is a string, albeit an empty one. How should the validation be formulated to allow empty strings?

2
  • Try adding the present or required validator as well Commented Feb 22, 2019 at 15:38
  • 4
    @ka_lin Present and required do not describe this property, which doesn't need to be present and is not required. Commented Feb 22, 2019 at 15:41

6 Answers 6

60

You would need nullable to allow an empty string

public function rules() 
{
    return [
        'age' => 'integer',
        'title' => 'nullable|string|max:50'
    ];
}
Sign up to request clarification or add additional context in comments.

5 Comments

nullable - The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.
This doesn't allow an empty string, this simply ignores an empty string, treating it as null. What if we want to overwrite the existing title field with an empty string?
@GluePear if you wanted to do that you would also need to disable \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class from your middleware. That middleware will automatically convert empty strings to null. From a database normalisation point of view, it would be better to have null as the value rather than an empty string anyway.
I confirm this does work. If you leave that specific field empty, nullable makes it pass being empty. Here are the docs.
Any solution in case you have $request->file('image')? Both nullable and sometimes doesn't work.
26

Try to see if ConvertEmptyStringsToNull middleware is active then it would explain this behavior, see docs

1 Comment

Thanks, that's useful. I'm not confident removing that middleware in case it's used elsewhere. But it explains the behaviour.
9

there is present rule in that check present of a key but let it to be empty.

#present

present The field under validation must be present in the input data but can be empty.

https://laravel.com/docs/5.7/validation#rule-present

Comments

2

Avoid touching Middleware settings.

Instead Use Laravel build in function to manipulate data before validation runs.

Inside Validation class

protected function prepareForValidation()
    {
        if($this->title == null )
            $this->merge(['title'=>'']);
    }

Comments

1

The accepted answer does not fix the issue when you have this rule:

return [
  "title" => "sometimes|string",
];

In this case, you need to specifiy the string is actually "nullable" (even if the ConvertEmptyStringsToNull middleware is active, tested on Laravel 8.77.1)

So this one will allow to pass an empty string on the "title" key:

return [
  "title" => "sometimes|string|nullable",
];

Comments

0

I will try

public function rules() 
{
    return [
        'age' => 'integer',
        'title' => 'string|sometimes'
    ];
}

This will only validate title when it is present.

1 Comment

Doesn't answer the problem when you want it to always be present, though allowed to be empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.