简体   繁体   English

Laravel old() 输入值在表单验证后总是返回 Null

[英]Laravel old() input values always returns Null after form validation

I have a basic form (available for auth users)我有一个基本表格(可供授权用户使用)

<form method="POST" action="/admin/insert">
@csrf  
<div class="form-group">
<label>{{ __($cv->name) }}</label>
<input type="text" name="name" class="form-control @error($ck) is-invalid @enderror"  value="{{ old('name') }}" >
</div>
<button type="submit" class="btn btn-primary me-2">{{ __('Submit') }}</button>
</form>

For testing purposes, I intentionally failed validation for form in controller.出于测试目的,我故意使 controller 中的表单验证失败。

if ($validator->fails()) {
    return back()->withErrors($validator)
        ->withInput($request->input());
}

My session friver is file:我的 session 是文件:

driver' => env('SESSION_DRIVER', 'file'),

Problem is that old('name') always returns NULL in blade, and It doesn't fills form value if validation failed.问题是old('name')总是在刀片中返回 NULL,如果验证失败,它不会填写表单值。

Where is the problem?问题出在哪儿? (Note: Login form work perfectly) (注意:登录表单完美运行)

The problem may be caused by the fact that you're using the file session driver.问题可能是由于您使用的是文件 session 驱动程序。 The file session driver stores session data in the file system, and when a user navigates away from a page or closes their browser, their session data is lost.文件 session 驱动程序在文件系统中存储 session 数据,当用户离开页面或关闭浏览器时,他们的 session 数据将丢失。 In this case, the old input function in your blade template is not able to retrieve the previously entered data from the session because the session data has been lost.在这种情况下,刀片模板中的旧输入 function 无法从 session 检索先前输入的数据,因为 session 数据已丢失。

To solve this, you can change your session driver to cookie, database or memcached/redis in your.env file.要解决这个问题,您可以在您的 .env 文件中将您的 session 驱动更改为 cookie、数据库或 memcached/redis。 These drivers will store session data on the user's browser as a cookie or in a database, allowing the old input function to retrieve the previously entered data even after the user navigates away from the page or closes their browser.这些驱动程序会将 session 数据作为 cookie 或数据库存储在用户的浏览器上,即使在用户离开页面或关闭浏览器后,也允许旧输入 function 检索先前输入的数据。

You should also make sure that your form includes the old input function where appropriate, that way the form will repopulate with the previously entered data if validation fails.您还应该确保您的表单在适当的地方包含旧输入 function,这样如果验证失败,表单将重新填充之前输入的数据。

Another cause of the problem might be related to a CSRF token problem.问题的另一个原因可能与 CSRF 令牌问题有关。 Since your form includes the @csrf directive, you will need to make sure that the CSRF_TOKEN variable is being set in the config/session.php file, or in case you have the variable defined in your.env file that the value is correct.由于您的表单包含 @csrf 指令,因此您需要确保在 config/session.php 文件中设置了 CSRF_TOKEN 变量,或者如果您在 .env 文件中定义了变量,则该值是正确的。

Finally, make sure you are using the POST method for form submission, since the old input function only retrieves data from the previous POST request, so in case you are doing a GET request the function will not retrieve the data from the previous request.最后,确保您使用的是 POST 方法提交表单,因为旧输入 function 仅从之前的 POST 请求中检索数据,因此如果您执行 GET 请求,function 将不会从之前的请求中检索数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM