英文:
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:
-
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. -
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.
-
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.
-
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. -
Custom Validation: If you have custom validation logic, ensure that it is working as expected and not causing any issues.
-
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->validate([
'first_name' => 'bail|required|max:35|regex:^[a-zA-Z]+$',
'middle_name' => 'bail|nullable|max:35|regex:^[a-zA-Z]+$',
'last_name' => 'bail|required|max:35|regex:^[a-zA-Z]+$',
'user_name' => 'bail|required|max:70|regex:^[a-zA-Z\s]*$',
'company_email' => 'bail|required|email|max:345|unique:users',
'password' => 'bail|required|min:8|max:255|regex:^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$',
'cnic' => 'bail|required|max:15|regex:\d{5}-\d{7}-\d',
'nationality' => 'bail|required|max:74|regex:^[a-zA-Z\s]*$',
'date' => 'bail|required|date',
'gender' => 'bail|required',
'marital_status' => 'bail|required',
'religion' => 'bail|required',
'current_address' => 'bail|required|max:255',
'permanant_address' => 'bail|required|max:255',
'personal_email' => 'bail|required|email|max:345|unique:users',
'primary_phone' => 'bail|required|max:15|regex:^(\+923)\d{9}$',
'secondary_phone' => 'bail|required|max:15|regex:^(\+923)\d{9}$',
'company_skype' => 'required',
'personal_skype' => 'nullable',
'linked_in' => 'bail|nullable|max:2000|url',
'github_link' => 'bail|nullable|max:2000|url',
'stackoverflow_link' => 'bail|nullable|max:2000|url',
'full_name' => 'bail|required|max:75|regex:^[a-zA-Z\s]*$',
'relation_name' => 'bail|required|max:255|regex:^[a-zA-Z\s]*$',
'branch' => 'required',
'phone_number' => 'bail|required|max:15|regex:^(\+923)\d{9}$',
'department' => 'required',
'designation' => 'required',
'level' => 'required',
'experience' => 'bail|required|max:4',
'join_date' => 'bail|required|date',
'account_status' => 'required'
]);
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) { ["bags":protected]=> array(1) { ["default"]=> object(Illuminate\Support\MessageBag)#246 (2) { ["messages":protected]=> array(10) { ["first_name"]=> array(1) { [0]=> string(33) "The first name field is required." } ["last_name"]=> array(1) { [0]=> string(32) "The last name field is required." } ["user_name"]=> array(1) { [0]=> string(32) "The user name field is required." } ["company_email"]=> array(1) { [0]=> string(36) "The company email field is required." } ["password"]=> array(1) { [0]=> string(31) "The password field is required." } ["cnic"]=> array(1) { [0]=> string(27) "The cnic field is required." } ["nationality"]=> array(1) { [0]=> string(34) "The nationality field is required." } ["personal_email"]=> array(1) { [0]=> string(37) "The personal email field is required." } ["primary_phone"]=> array(1) { [0]=> string(36) "The primary phone field is required." } ["secondary_phone"]=> array(1) { [0]=> string(38) "The secondary phone field is required." } } ["format":protected]=> string(8) ":message" } } }
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) { ["bags":protected]=> 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->all(), [
'first_name' => 'required|max:35|regex:^[a-zA-Z]+$',
'middle_name' => 'nullable|max:35|regex:^[a-zA-Z]+$',
'last_name' => 'required|max:35|regex:^[a-zA-Z]+$',
'user_name' => 'required|max:70|regex:^[a-zA-Z\s]*$',
'company_email' => 'required|email|max:345|unique:users',
'password' => 'required|min:8|max:255|regex:^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$',
'cnic' => 'required|max:15|regex:\d{5}-\d{7}-\d',
'nationality' => 'required|max:74|regex:^[a-zA-Z\s]*$',
// 'date' => 'required|date',
// 'gender' => 'required',
// 'marital_status' => 'required',
// 'profile_picture' => 'nullable|image|mimes:jpeg,png,jpg|max:2000000',
'personal_email' => 'bail|required|email|max:345|unique:users',
'primary_phone' => 'bail|required|max:15|regex:^(\+923)\d{9}$',
'secondary_phone' => 'bail|required|max:15|regex:^(\+923)\d{9}$',
// 'current_address' => 'required|max:255',
'permanant_address' => 'required|max:255',
// 'religion' => 'required',
]);
if ($validator->fails()) {
return redirect('/user/create')->withErrors($validator)->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 -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论