【问题标题】:How to check if the request field is empty in laravel controller?如何检查 laravel 控制器中的请求字段是否为空?
【发布时间】:2020-06-05 19:14:55
【问题描述】:

我有一个名为“min”的表单输入,在我的控制器中,我使用了验证数字,用户必须在其中只输入数字,如果用户将其留空,则将“minimum”的值设置为零并发布到我的数据库

这是我的控制器:

 $rules = [
        'user_id' => '',
        'name' => 'required|max:255|min:2',
        'description' => 'required|min:10|max:1000',
        'price' => 'required|numeric',
        'min' => 'numeric',
    ];
    $errormessages = [
        'required' => ':attribute is required !',
        'max' => ':attribute max size exceded ',
        'min' => ':attribute is too short',
        'numeric' => ':attribute must be numeric',

    ];

    $this->validate($request, $rules, $errormessages);

$userproduct = new Userproduct();
    $userproduct->user_id = auth()->id();
    $userproduct->name = $request->name;
    $userproduct->description = $request->description;
    $userproduct->price = $request->price;
    if (!empty($request->input('min')))
    {
        $userproduct->min = $request->min;
    }
    else {
        $userproduct->min = 0;
    }
$userproduct->save();
    return redirect('/profile/products')->with('message', 'Added sucsessfuly');

我尝试了太多方法,例如: if ($request->has('min') if ($request->filled('min')

但是当我提交表单时,它给了我一个错误“最小值必须是数字”,它似乎仍然在发布 null!

请帮我解决这个问题

【问题讨论】:

  • 使用nullable 规则接受空字符串。
  • @Majid 这仅仅意味着您正在使用 来接受控制器中的数字。这不起作用,您应该将表单字段类型更改为“数字”或更改控制器规则。

标签: laravel


【解决方案1】:

嘿,您需要在验证规则中添加可空值

像这样:

$rules = [
    'min' => 'nullable|numeric',
];

Laravel 自动将空字符串转换为 nullnull 不是数值

现在它应该接受数值和一个空字段

【讨论】:

  • 我不想为空!请再次查看我的问题,我只想如果用户将“min”字段留空,将“minimum”的值设置为零并发布到我的数据库
  • 您似乎不明白您的问题是什么。试试看它是否符合您的需求。
【解决方案2】:
 $validator = Validator::make($request->all(), [
            'user_id' => '',
            'name' => 'required|max:255|min:2',
            'description' => 'required|min:10|max:1000',
            'price' => 'required|numeric',
            'min' => 'nullable|numeric',
        ],[
        'required' => ':attribute is required !',
        'max' => ':attribute max size exceded ',
        'min' => ':attribute is too short',
        'numeric' => ':attribute must be numeric',

    ]);

if (!$validator->fails()) {

   $userproduct->min = isset($request->min)?$request->min:0;
}

【讨论】:

    【解决方案3】:

    发生错误numeric验证需要更新控制器代码:

    添加nullablenumeric

    $rules = [
       'user_id' => '',
        'name' => 'required|max:255|min:2',
        'description' => 'required|min:10|max:1000',
        'price' => 'required|numeric',
        'min' => 'numeric|nullable',
     ];
     $errormessages = [
        'required' => ':attribute is required !',
        'max' => ':attribute max size exceded ',
        'min' => ':attribute is too short',
        'numeric' => ':attribute must be numeric',
    ];
    
    $this->validate($request, $rules, $errormessages);
    
    $userproduct = new Userproduct();
    $userproduct->user_id = auth()->id();
    $userproduct->name = $request->name;
    $userproduct->description = $request->description;
    $userproduct->price = $request->price;
    
    //Here you already wrote logic for set 0 value.
    if (!empty($request->input('min'))){
        $userproduct->min = $request->min;
    } else {
        $userproduct->min = 0;
    }
    $userproduct->save();
    return redirect('/profile/products')->with('message', 'Added sucsessfuly');
    

    【讨论】:

    • 我不想为空!请再次查看我的问题,我只想在用户将“min”字段留空时将“minimum”的值设置为零并发布到我的数据库
    • @MajidAdigozalpour 不不,你理解错了,因为nullable 表示当Validation 执行时空值接受,然后在你的控制器中你已经为min 值集0 编写了逻辑。我更新了我的答案。
    • @MajidAdigozalpour 我已经测试过了,它在我的 laravel 安装中运行良好,所以请尝试一下..
    【解决方案4】:

    在 laravel ->get() 方法有效

    试试这个

    $request->get('min')
    

    【讨论】:

    • 请解释更多
    【解决方案5】:

    用途:

    $rules = [
        'min' => 'nullable|numeric',
    ];
    
    

    如果为空,则设置零(0)只是说:

    $min = $request->input('min', 0) ; // in this case 0 is the default value
    
    

    【讨论】:

    • 谢谢,但它不起作用!提交的表单,但在我的数据库中 min 的值设置为 NULL!
    • 问题是验证规则“可为空”允许表单发布空数据,否则会出错。激活该规则后,您可以检查空值。
    • 如果“可为空”在您的验证规则中,只需执行以下操作: $userproduct->min = is_null(request->min) ? 0 : $request->min;
    猜你喜欢
    • 2017-07-02
    • 2013-08-11
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2018-03-05
    相关资源
    最近更新 更多