简体   繁体   English

Laravel 表单验证问题(会话大小限制?)

[英]Laravel form validation issues (session size limit?)

I have a form with about 30 inputs that should be validated.我有一个包含大约 30 个应该验证的输入的表单。 This is quite fresh Laravel installation and I handle the validation in controller.这是全新的 Laravel 安装,我在控制器中处理验证。 It's a simple project and everything is basicly from Laravel docs, so there's not much to show you when it comes to the code itself.这是一个简单的项目,所有内容基本上都来自 Laravel 文档,因此当涉及到代码本身时,没有太多可向您展示的内容。

The issue has something to do with the amount of failing fields or the amount of rules, but I can't get my head around it.这个问题与失败字段的数量或规则的数量有关,但我无法理解。

If I submit the form without filling any of the input fields, validation fails and I get redirected back to the form, BUT without validation errors OR old input data.如果我在没有填写任何输入字段的情况下提交表单,验证将失败并且我被重定向回表单,但没有验证错误或旧的输入数据。 Same thing if I fill one input and then submit, I'm redirected back without errors or old value of the input I filled.同样的事情,如果我填写一个输入然后提交,我被重定向回来,没有错误或我填写的输入的旧值。

However,然而,

  1. If I fill 7 input fields and submit, everything works.如果我填写 7 个输入字段并提交,则一切正常。 After redirection, those 7 fields has old values and the rest has error messages.重定向后,这 7 个字段具有旧值,其余字段具有错误消息。
  2. If I comment out 7 rules from the validation rule array, it obviously works the same way as above.如果我从验证规则数组中注释掉 7 条规则,它的工作方式显然与上面相同。
  3. If I use Validator::make and manually redirect back and use ->withErrors($validator) , the errors shows at the form.如果我使用Validator::make并手动重定向回来并使用->withErrors($validator) ,错误会显示在表单中。 The same way, if I use ->withInput() on the redirect, form will have all the old data.同样,如果我在重定向上使用->withInput() ,表单将拥有所有旧数据。 However, using both of these together, wont return errors nor old data back to form.但是,同时使用这两者,不会返回错误或旧数据返回表单。

When this issue happens, debug bar shows array:2 [ "old" => [] "new" => [] ] for _flash and session doesn't seem to have errors or _old_input defined at all.发生此问题时,调试栏显示array:2 [ "old" => [] "new" => [] ] for _flash并且会话似乎根本没有定义errors_old_input

This is 100% consistent, so it's definitely the amount that causes it, no matter which rules or fields I fill or comment out.这是 100% 一致的,因此无论我填写或注释哪些规则或字段,这绝对是导致它的数量。

It happens indentically with both $request->validate($myRules);它同时发生在$request->validate($myRules); and $v = Validator::make($request->all(), $myRules);$v = Validator::make($request->all(), $myRules);

I haven't found much from Google regarding this kinda of behaviour.我还没有从谷歌找到太多关于这种行为的信息。 Just in case, I've changed session driver to file , but it didn't help.以防万一,我已将 session driver 更改为file ,但这没有帮助。

I'm currently running on XAMP.我目前正在 XAMP 上运行。 ( memory_limit=512M ) memory_limit=512M

Edit :编辑

My final validation logic involves some "if-situations", like:我的最终验证逻辑涉及一些“if-situations”,例如:

$v->sometimes('description', 'required|max:500', function ($input){
    return $input->publish == 1;
});

And now when I've utilized these situations, I noticed that they actually decrease the sketchy "limit" of my issue.现在,当我利用这些情况时,我注意到它们实际上减少了我问题的粗略“限制”。 So by adding this single sometimes-condition and removing the description rule from main rule array, I only need to fill 6 fields anymore to make everything work.因此,通过添加这个单一的有时条件并从主规则数组中删除description规则,我只需要再填充 6 个字段即可使一切正常。

In the end the form will surely have required attributes set to the inputs, so this issue wont happen in real-life scenario, yet it still annoys me a lot, as there must be something causing this and later on it might cause something else too.最后表单肯定会为输入设置必需的属性,所以这个问题不会在现实生活中发生,但它仍然让我很恼火,因为一定有什么原因导致了这个,后来它也可能会导致其他问题.

Edit2 :编辑2

I made a new form, with 22 inputs and named them with logic: f1, f2, f3, and so on, and it works perfectly!我做了一个新表单,有 22 个输入,并用逻辑命名它们:f1、f2、f3 等等,它完美地工作!

But updated few of them with a bit longer names, like: f1, f2long, f3, f4evenlonger, etc, and suddenly the problem came back.但是用更长的名称更新了其中的几个,例如:f1、f2long、f3、f4evenlonger 等,突然问题又回来了。 Then I removed few characters again, and found a spot where it sometimes works and sometimes doesn't.然后我再次删除了几个字符,并找到了一个有时有效有时无效的地方。

If the names of the input fields has 57 characters in total , everything works 100%.如果输入字段的名称总共有 57 个字符,则一切正常 100%。 If I add few characters and this number goes to 63, it becomes unstable and sometimes works - sometimes doesn't.如果我添加几个字符并且这个数字变为 63,它会变得不稳定并且有时会起作用 - 有时不会。 If there's total of 70 or more characters, it never works.如果总共有 70 个或更多字符,它永远不会工作。

So clearly something limits it, but what?很明显有些东西限制了它,但是什么?

Edit3 :编辑3

Huh, it's session size limit.. Damn it took long time to realize it.嗯,这是会话大小限制..该死的花了很长时间才意识到这一点。

I defined "file" as session driver, yet it doesn't generate any files in the location where it should, so it's still apparently using "cookie".我将“文件”定义为会话驱动程序,但它没有在它应该生成的位置生成任何文件,因此它显然仍在使用“cookie”。 I gave Win10 users the rights to write files in the storage folder (and sub folders), but it still wont generate anything there.我给了 Win10 用户在存储文件夹(和子文件夹)中写入文件的权限,但它仍然不会在那里生成任何内容。

In the end it happened becouse the session data didn't fit into a cookie.最后它发生是因为会话数据不适合 cookie。 After long debugging (I was sure the issue was completely elsewhere) I changed my session driver to file inside session.php , but forgot to update .env ..经过长时间的调试(我确定问题完全出在其他地方)我将会话驱动程序更改为session.php file ,但忘记更新.env ..

So now everything works, by using file and/or database as session driver.所以现在一切正常,通过使用file和/或database作为会话驱动程序。

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

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