validate()方法在Laravel 6中有一个可以重定向的错误限制吗?

huangapple go评论65阅读模式
英文:

Does validate() method have a limit on errors it can redirect back in Laravel 6?

问题

The validate() method in Laravel does not have a specific limit on the number of errors it can redirect back with. It will validate all the specified fields and collect all validation errors encountered during the validation process.

If you're experiencing issues where it seems to stop working when you have many fields, there could be a few reasons for this behavior:

  1. Server Configuration: Check your server configuration, especially the PHP max_input_vars setting. If you have a large number of input fields, you might need to increase this limit to accommodate all the fields being submitted.

  2. Validation Rules: Ensure that your validation rules are correct for each field. Any rule violation will result in a validation error. Make sure you have the correct rules and that they match your actual data.

  3. Resource Constraints: Depending on your server's resources, a very large number of fields may cause performance issues. Make sure your server has enough memory and processing power to handle the validation for all the fields.

  4. Input Data: Check the data you are sending to the validate() method. If there are any unexpected or incorrect values in the input data, it can lead to validation errors.

  5. Custom Validation: If you have custom validation logic, ensure that it is working as expected and not causing any issues.

  6. Middleware: Review any middleware you are using in your application. Some middleware might affect the validation process.

Without specific error messages or logs, it's challenging to pinpoint the exact issue you're facing. You mentioned that your Laravel logs don't show any errors, but it's essential to double-check and ensure that you are logging the errors correctly.

If you continue to experience issues, consider breaking down the validation into smaller parts or groups of fields to isolate the problem and make debugging easier.

英文:

Does the validate() method have a limit on errors that it can redirect back to Laravel? Below is my validation method. It is not working with this many fields. However, if I comment out some fields than it starts working. I suspected that some fields might contain syntax error but that is not the case since I checked all of them separately. Can anyone point me to the problem?

<!-- language: php -->

$validatedData = $request-&gt;validate([
    &#39;first_name&#39; =&gt; &#39;bail|required|max:35|regex:^[a-zA-Z]+$&#39;,
    &#39;middle_name&#39; =&gt; &#39;bail|nullable|max:35|regex:^[a-zA-Z]+$&#39;,
    &#39;last_name&#39; =&gt; &#39;bail|required|max:35|regex:^[a-zA-Z]+$&#39;,
    &#39;user_name&#39; =&gt; &#39;bail|required|max:70|regex:^[a-zA-Z\s]*$&#39;,
    &#39;company_email&#39; =&gt; &#39;bail|required|email|max:345|unique:users&#39;,
    &#39;password&#39; =&gt; &#39;bail|required|min:8|max:255|regex:^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&amp;])[A-Za-z\d@$!%*?&amp;]{8,}$&#39;,
    &#39;cnic&#39; =&gt; &#39;bail|required|max:15|regex:\d{5}-\d{7}-\d&#39;,
    &#39;nationality&#39; =&gt; &#39;bail|required|max:74|regex:^[a-zA-Z\s]*$&#39;,
    &#39;date&#39; =&gt; &#39;bail|required|date&#39;,
    &#39;gender&#39; =&gt; &#39;bail|required&#39;,
    &#39;marital_status&#39; =&gt; &#39;bail|required&#39;,
    &#39;religion&#39; =&gt; &#39;bail|required&#39;,
    &#39;current_address&#39; =&gt; &#39;bail|required|max:255&#39;,
    &#39;permanant_address&#39; =&gt; &#39;bail|required|max:255&#39;,
    &#39;personal_email&#39; =&gt; &#39;bail|required|email|max:345|unique:users&#39;,
    &#39;primary_phone&#39; =&gt; &#39;bail|required|max:15|regex:^(\+923)\d{9}$&#39;,
    &#39;secondary_phone&#39; =&gt; &#39;bail|required|max:15|regex:^(\+923)\d{9}$&#39;,
    &#39;company_skype&#39; =&gt; &#39;required&#39;,
    &#39;personal_skype&#39; =&gt; &#39;nullable&#39;,
    &#39;linked_in&#39; =&gt; &#39;bail|nullable|max:2000|url&#39;,
    &#39;github_link&#39; =&gt; &#39;bail|nullable|max:2000|url&#39;,
    &#39;stackoverflow_link&#39; =&gt; &#39;bail|nullable|max:2000|url&#39;,
    &#39;full_name&#39; =&gt; &#39;bail|required|max:75|regex:^[a-zA-Z\s]*$&#39;,
    &#39;relation_name&#39; =&gt; &#39;bail|required|max:255|regex:^[a-zA-Z\s]*$&#39;,
    &#39;branch&#39; =&gt; &#39;required&#39;,
    &#39;phone_number&#39; =&gt; &#39;bail|required|max:15|regex:^(\+923)\d{9}$&#39;,
    &#39;department&#39; =&gt; &#39;required&#39;,
    &#39;designation&#39; =&gt; &#39;required&#39;,
    &#39;level&#39; =&gt; &#39;required&#39;,
    &#39;experience&#39; =&gt; &#39;bail|required|max:4&#39;,
    &#39;join_date&#39; =&gt; &#39;bail|required|date&#39;,
    &#39;account_status&#39; =&gt; &#39;required&#39;
]);

I am actually dumping the errors using var_dump().
This is what i am getting when i comment most of the key/value pairs in my array which passed to the validate method.

object(Illuminate\Support\ViewErrorBag)#245 (1) { [&quot;bags&quot;:protected]=&gt; array(1) { [&quot;default&quot;]=&gt; object(Illuminate\Support\MessageBag)#246 (2) { [&quot;messages&quot;:protected]=&gt; array(10) { [&quot;first_name&quot;]=&gt; array(1) { [0]=&gt; string(33) &quot;The first name field is required.&quot; } [&quot;last_name&quot;]=&gt; array(1) { [0]=&gt; string(32) &quot;The last name field is required.&quot; } [&quot;user_name&quot;]=&gt; array(1) { [0]=&gt; string(32) &quot;The user name field is required.&quot; } [&quot;company_email&quot;]=&gt; array(1) { [0]=&gt; string(36) &quot;The company email field is required.&quot; } [&quot;password&quot;]=&gt; array(1) { [0]=&gt; string(31) &quot;The password field is required.&quot; } [&quot;cnic&quot;]=&gt; array(1) { [0]=&gt; string(27) &quot;The cnic field is required.&quot; } [&quot;nationality&quot;]=&gt; array(1) { [0]=&gt; string(34) &quot;The nationality field is required.&quot; } [&quot;personal_email&quot;]=&gt; array(1) { [0]=&gt; string(37) &quot;The personal email field is required.&quot; } [&quot;primary_phone&quot;]=&gt; array(1) { [0]=&gt; string(36) &quot;The primary phone field is required.&quot; } [&quot;secondary_phone&quot;]=&gt; array(1) { [0]=&gt; string(38) &quot;The secondary phone field is required.&quot; } } [&quot;format&quot;:protected]=&gt; string(8) &quot;:message&quot; } } } 

When i uncomment all of the keys of array i am passing to the validate array this is what i get:

object(Illuminate\Support\ViewErrorBag)#247 (1) { [&quot;bags&quot;:protected]=&gt; array(0) { } }

I also tried Validate::make method but the behavior was still the same and i am not able to resolve the issue.

 $validator = Validator::make($request-&gt;all(), [
            &#39;first_name&#39; =&gt; &#39;required|max:35|regex:^[a-zA-Z]+$&#39;,
            &#39;middle_name&#39; =&gt; &#39;nullable|max:35|regex:^[a-zA-Z]+$&#39;,
            &#39;last_name&#39; =&gt; &#39;required|max:35|regex:^[a-zA-Z]+$&#39;,
            &#39;user_name&#39; =&gt; &#39;required|max:70|regex:^[a-zA-Z\s]*$&#39;,
            &#39;company_email&#39; =&gt; &#39;required|email|max:345|unique:users&#39;,
            &#39;password&#39; =&gt; &#39;required|min:8|max:255|regex:^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&amp;])[A-Za-z\d@$!%*?&amp;]{8,}$&#39;,
            &#39;cnic&#39; =&gt; &#39;required|max:15|regex:\d{5}-\d{7}-\d&#39;,
            &#39;nationality&#39; =&gt; &#39;required|max:74|regex:^[a-zA-Z\s]*$&#39;,
            // &#39;date&#39; =&gt; &#39;required|date&#39;,
            // &#39;gender&#39; =&gt; &#39;required&#39;,
            // &#39;marital_status&#39; =&gt; &#39;required&#39;,
            // &#39;profile_picture&#39; =&gt; &#39;nullable|image|mimes:jpeg,png,jpg|max:2000000&#39;,
            &#39;personal_email&#39; =&gt; &#39;bail|required|email|max:345|unique:users&#39;,
            &#39;primary_phone&#39; =&gt; &#39;bail|required|max:15|regex:^(\+923)\d{9}$&#39;,
            &#39;secondary_phone&#39; =&gt; &#39;bail|required|max:15|regex:^(\+923)\d{9}$&#39;,

            // &#39;current_address&#39; =&gt; &#39;required|max:255&#39;,
            &#39;permanant_address&#39; =&gt; &#39;required|max:255&#39;,
            // &#39;religion&#39; =&gt; &#39;required&#39;,
        ]);

        if ($validator-&gt;fails()) {
            return redirect(&#39;/user/create&#39;)-&gt;withErrors($validator)-&gt;withInput();
        }

My Laravel.logs file doesnot have any error too.

答案1

得分: 1

我有同样的问题。
也许这是解决方法。

.env
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->

<!-- 语言:lang-html -->

SESSION_DRIVER=file

<!-- 结束代码片段 -->

英文:

i have same problem.
maybe this is solution

.env
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

SESSION_DRIVER=file

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月6日 15:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定