Identifizieren Sie Probleme mit dieser Formularvalidierung
P粉296080076
P粉296080076 2023-08-31 17:10:00
0
2
486

Ich verwende Laravel 5.8 und habe diese Controller-Methode erstellt, um einige Datensätze in der Datenbank zu erstellen.

public function doTheUpload(Request $request) { versuchen{ $request->validate([ 'video' => 'nullable|mimes:mp4', 'video_thumb' => 'required|mimes:jpg,png,jpeg', 'video_name' => 'erforderlich', 'video_desc' => 'available_download' => 'nullable', ],[ 'video.mimes' => 'Videodateiformat ist ungültig', 'video_thumb.required' => 'Das Hochladen der Miniaturansicht des Videos ist erforderlich', 'video_name.required' => 'Sie müssen den Namen des Videos eingeben', 'video_thumb.mimes' => 'Bildminiaturdateiformat ist ungültig', ]); //Führen Sie den Upload-Vorgang durch }catch(Ausnahme $e){ dd($e); } }

Aber das funktioniert nicht und gibt diesen Fehler zurück:

Die angegebenen Daten sind ungültig.

Das liegt im Wesentlichen an den Formularvalidierungsanforderungen und wenn ich diese Validierungen aus der Methode entferne, funktioniert es einwandfrei.

Was ist also falsch an der Validierung der Formularanfragen, die diesen Fehler zurückgeben?

Wenn Sie es wissen, lassen Sie es mich bitte wissen... Ich würde mich über jeden Gedanken oder Vorschlag sehr freuen.

Vielen Dank.

P粉296080076
P粉296080076

Antworte allen (2)
P粉569205478

您必须准确指定要验证的内容:

$request->validate([ 'request.video' => 'nullable|mimes:mp4', 'request.video_thumb' => 'required|mimes:jpg,png,jpeg', 'request.video_name' => 'required', 'request.video_desc' => 'nullable', 'request.available_download' => 'nullable', ], [ 'request.video.mimes' => 'video file format is not valid', 'request.video_thumb.required' => 'uploading video thumbnail is required', 'request.video_name.required' => 'you must enter name of video', 'request.video_thumb.mimes' => 'image thumbnail file format is not valid', ]);

我的代码示例:

$requestData = $request->request_data; // data $company_name = $requestData['company_name']; $company_type = $requestData['company_type']; $company_address = $requestData['company_address']; $latitude = $requestData['latitude']; $longitude = $requestData['longitude']; $company_branch_count = $requestData['company_branch_count']; $yes_radio = strval($requestData['yes_radio']); $no_radio = strval($requestData['no_radio']); $company_contact_user_first_name = $requestData['company_contact_user_first_name']; $company_contact_user_last_name = $requestData['company_contact_user_last_name']; $company_contact_user_email = $requestData['company_contact_user_email']; $company_contact_user_password = $requestData['company_contact_user_password']; $company_contact_user_phone = $requestData['company_contact_user_phone']; $company_kvkk_ok = strval($requestData['company_kvkk_ok']); $shipping_method_yourself = $yes_radio === 'true' && $yes_radio != $no_radio ? 1 : 0; if ($company_kvkk_ok == 'false') { return json_encode([ 'operation_status' => 'error', 'error_messages' => 'no', ]); } // Validate $validator = Validator::make($request->all(), [ "request_data.company_name" => "required|string|min:5", "request_data.company_type" => "required|in:0,1,2,3,4,5,6,7,8,9,10,11,12", "request_data.company_address" => "required", "request_data.latitude" => "required", "request_data.longitude" => "required", "request_data.company_branch_count" => "required|integer", "request_data.yes_radio" => "required", "request_data.no_radio" => "required", "request_data.company_contact_user_first_name" => "required", "request_data.company_contact_user_last_name" => "required", "request_data.company_contact_user_email" => [ 'required', 'email', Rule::unique('users', 'email')->where(function ($query) use ($company_contact_user_email) { return $query->where('email', $company_contact_user_email); }), Rule::unique('companies', 'company_contact_user_email')->where(function ($query) use ($company_contact_user_email) { return $query->where('company_contact_user_email', $company_contact_user_email); }), ], "request_data.company_contact_user_password" => "required|min:6", "request_data.company_contact_user_phone" => "required", "request_data.company_kvkk_ok" => "required", ], [ 'request_data.company_name.required' => __('company name required'), 'request_data.company_name.string' => __('company name must be string'), 'request_data.company_name.min' => __('company name must be at least 5 characters'), 'request_data.company_type.required' => __('company type required'), 'request_data.company_type.in' => __('company type invalid'), 'request_data.company_address.required' => __('company address required'), 'request_data.latitude.required' => __('latitude required'), 'request_data.longitude.required' => __('longitude required'), 'request_data.company_branch_count.required' => __('company branch count required'), 'request_data.company_branch_count.integer' => __('company branch count must be integer'), 'request_data.yes_radio.required' => __('yes radio required'), 'request_data.no_radio.required' => __('no radio required'), 'request_data.company_contact_user_first_name.required' => __('company contact user first name required'), 'request_data.company_contact_user_last_name.required' => __('company contact user last name required'), 'request_data.company_contact_user_email.required' => __('company contact user email required'), 'request_data.company_contact_user_email.email' => __('company contact user email invalid'), 'request_data.company_contact_user_email.unique' => __('email already taken'), 'request_data.company_contact_user_password.required' => __('company contact user password required'), 'request_data.company_contact_user_password.min' => __('company contact user password must be at least 6 characters'), 'request_data.company_contact_user_phone.required' => __('company contact user phone required'), 'request_data.company_kvkk_ok.required' => __('company kvkk ok required'), ]); if ($validator->fails()) { $messages = $validator->messages(); return json_encode([ 'operation_status' => 'not_validated', 'request' => $requestData, 'messages' => $messages, ]); }
    P粉278379495

    当你使用 Laravel 的验证时,你应该让 Laravel 处理错误,因为当规则失败时,Laravel 会自动抛出异常。 因此,第一个建议是不要在验证例程中使用 try-catch 块。

    正如 Laravel 文档所述: p>

    此外,我建议您不要在控制器中使用验证,因为根据良好实践,建议创建单独的 formRequest 进行验证,因此您应该稍微修改控制器以包含验证器类:

    
              

    现在您必须创建一个表单请求,也许使用 php artisan make:request UploadVideoRequest 此命令将在app/Http/Requests下创建一个表单请求类,您应将其填充为:

     'nullable|mimes:mp4', 'video_thumb' => 'required|mimes:jpg,png,jpeg', 'video_name' => 'required', 'video_desc' => 'nullable', 'available_download' => 'nullable', ]; } /** * Define messages to return if an error is detected. * * @return array */ public function messages() { return [ 'video.mimes' => 'video file format is not valid', 'video_thumb.required' => 'uploading video thumbnail is required', 'video_name.required' => 'you must enter name of video', 'video_thumb.mimes' => 'image thumbnail file format is not valid', ]; } }

    通过使用这种方法,Laravel 验证用户输入并通过异常管理任何错误。

    问候。

      Neueste Downloads
      Mehr>
      Web-Effekte
      Quellcode der Website
      Website-Materialien
      Frontend-Vorlage
      Über uns Haftungsausschluss Sitemap
      Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!