如何解决Laravel中必填字段的问题?

时间:2019-02-10 16:19:10

标签: php laravel validation null laravel-5.7

当我提交按钮时,它会显示(名称和电子邮件)。我已经尝试了很多次,但是无法解决这个问题。

    class ContactController extends Controller
    {
        /**
         * @param Request $request
         *
         * @return \Illuminate\Http\RedirectResponse
         * @throws \Illuminate\Validation\ValidationException
         */
        public function sendMessage(Request $request) {
            $this->validate($request, [
                'name' => 'required',
                'email' => 'required',
                'subject' => 'required',
                'message' => 'required'
            ]);

            $contact = new Contact();
            $contact->name = $request->name;
            $contact->email = $request->email;
            $contact->subject = $request->subject;
            $contact->message = $request->message;
            $contact->save();

            Toastr::success('Your message successfully send.', 'Success', ["positionClass" => "toast-top-right"]);
            return redirect()->back();
        }
    }

向上方法:

public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('subject');
            $table->text('message');
            $table->timestamps();
        });
    }

这是HTML代码:

<form class="contact-form" method="post" action="{{ route('contact.send') }}">
                        @csrf
                        <div class="col-md-6 col-sm-6">
                            <div class="form-group">
                                <input  name="name" type="text" class="form-control" id="name" placeholder="  Name">
                            </div>
                            <div class="form-group">
                                <input name="email" type="email" class="form-control" id="email" placeholder="  Email">
                            </div>
                            <div class="form-group">
                                <input name="subject" type="text" class="form-control" id="subject" placeholder="  Subject">
                            </div>
                        </div>

                        <div class="col-md-6 col-sm-6">
                            <textarea name="message" type="text" class="form-control" id="message" rows="7" placeholder="  Message"></textarea>
                        </div>

                        <div class="col-md-6 col-md-offset-3 col-sm-6 col-sm-offset-3">
                            <div class="text-center">
                                <button type="submit" id="submit" name="submit" class="btn btn-send">Send </button>
                            </div>
                        </div>
                    </form>

Result

1 个答案:

答案 0 :(得分:0)

如果要传递名称和电子邮件的值,您能否在请求有效负载中签入Laravel调试工具栏?