PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 13, 2022

[FIXED] Laravel string validation to allow empty strings

 January 13, 2022     laravel, php, validation     No comments   

Issue

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?


Solution

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



Answered By - ka_lin
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing