Glu*_*ear 3 php validation laravel
在Laravel 5.7中,我正在使用表单请求验证:
public function rules() 
{
    return [
        'age' => 'integer',
        'title' => 'string|max:50'
    ];
}
如果我使用此有效负载向我的API提交请求:
{
  "age": 24,
  "title": ""
}
Laravel返回错误:
{
    "message": "The given data was invalid.",
    "errors": {
        "title": [
            "The title must be a string."
        ]
    }
}
我希望它能通过验证,因为标题是一个字符串,尽管是空的。验证应如何制定以允许使用空字符串?
您需要nullable允许一个空字符串
public function rules() 
{
    return [
        'age' => 'integer',
        'title' => 'nullable|string|max:50'
    ];
}
有一个present规则是检查密钥是否存在,但让它为空。
#展示
存在 正在验证的字段必须存在于输入数据中,但可以为空。
https://laravel.com/docs/5.7/validation#rule-present