在 Laravel 8 中验证

Validation in Laravel 8

我正在尝试在 Laravel 8 中创建个性化请求。

class SendContactFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|min:4|max:30',
            'email' => 'required|email',
            'phone' => 'required|numeric|size:11',
            'message' => 'required|min:4|max:400',
        ];
    }
}

在我的控制器中,我用它来发送电子邮件。

public function sendContactForm(SendContactFormRequest $request)
{
    try {
        $data = [
            'name' => $request->get('name'),
            'email' => $request->get('email'),
            'phone' => $request->get('phone'),
            'message' => $request->get('message'),
        ];
        // SEND EMAIL
        $this->sendNotification($data);

        return redirect()
            ->back()
            ->with('success', trans('web.'));

    } catch (Exception $e) {
        return redirect()
            ->back()
            ->with('danger', $e->getMessage());
    }
}

但总是 return HTTP ERROR 500 我不知道我做错了什么...我在看任何教程和任何代码示例,但我不知道它是什么这是我的问题。

已更新

最后我这样做了:

首先我要创建一个 personalised request:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;


class SendContactFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        
        return [
            'name'    => 'required|string|max:20',
            'email'   => 'required|email|unique:users,email',
            'phone'   => 'required|numeric|min:10',
            'message' => 'required|string|max:400',
        ];
    }
}

在我的控制器中,我添加 use 并在函数中创建对象:

public function sendContactForm(SendContactFormRequest $request)
    {
        try{

            $data = [
                'name'      => $request->name,
                'email'     => $request->email,
                'phone'     => $request->phone,
                'message'   => $request->message,
            ];
            
            // SEND EMAIL
            $this->sendNotification($data);

            return redirect()
                ->back()
                ->with('success', trans('web.'));
        
        }catch (Exception $e) {
            return redirect()
                ->back()
                ->with('danger', $e->getMessage());
        }
    }

现在我是用户 $request->variable,在我使用 $request->get() 之前,我配置了我的通知并发送了我的电子邮件。

现在我的问题是我托盘发送我的联系表格为空,我无法显示消息...但现在我可以看到我的 apache 日志,我有这个:

[

Mon May 03 17:55:48.109395 2021] [php:error] [pid 9796:tid 1216] [client ::1:61899] PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 262144 bytes) in C:\wamp64\www\aeveWeb\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php on line 129, referer: http://aeveweb.local/contact

我在 wampServer 中将该值增加到 512MB,但结果相同。我认为我的代码没有验证我的表单,但我不明白我不知道我做错了

更新

函数发送通知

/**
     * SEND NOTIFICATION WHEN CONTACT FORM IT´S SEND
     */
    public function sendNotification($data)
    {
        $emailTo = "";
        $details = [
            'name'      => $data["name"],
            'email'     => $data["email"],
            'phone'     => $data["phone"],
            'message'   => $data["message"],
        ];

        Notification::route('mail', $emailTo)->notify(new newMessage($details));
        
       
        return redirect()->back()->with('success', 'Notificación enviada');
    }

我通过以下方式解决我的问题:

$data = [
                'name'      => $request->name,
                'email'     => $request->email,
                'phone'     => $request->phone,
                'message'   => $request->message,
            ];

            $validator = Validator::make($data,[
                'name'      => 'required|string|min:3|max:125',
                'email'     => 'required|string|email|max:100',
                'phone'     => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:9',
                'message'   => 'required|string|max:400'
            ]);

            if ($validator->fails()) {
                return back()->withErrors($validator);
            }else{
    
                // SEND EMAIL
                $this->sendNotification($data);
    
                return redirect()
                    ->back()
                    ->with('success', trans('web.contact_form_send'));
            }