Laravel 5.8可邮寄的多种形式验证

时间:2019-08-05 15:44:45

标签: php laravel laravel-validation laravel-mail

我有一个带有多种表单的Laravel页面,该页面需要发送并且也要经过验证。每次我提交“ Form2”时,它都会提交并验证“ form1”

我尝试将其包装在此

if($request->get('submit')){
    //do something
}

如果我删除其他路由,则任何一种形式均有效,而另一种形式则具有正确的验证。

Route::post('/', function(Request $request ) {
    Mail::send(new ContactMail($request));
    return redirect('/');
});

但这似乎无济于事。

home.blade.php

<form method="POST" action="{{ url('/') }}">
    @csrf

        <div class="px-2">
            <div class="flex-wrap xs:flex-wrap sm:flex-wrap md:flex lg:flex xl:flex -mx-2">
                <div class="w-full xs:w-full sm:w-full md:w-1/2 lg:w-1/2 xl:w-1/2 px-2">
                <div class="py-4 xs:py-4 sm:py-4 md:py-0 lg:py-0 xl:py-0">
                    <label class="block">
                        <span class="text-green-500">First Name*</span>
                        <input name="fname" value="{{ old('fname') }}" class="@error('fname') is-invalid @enderror mt-1 block w-full border-green-500 border-b-2 bg-grey-500 py-1 text-green-900 text-3xl font-century-gothic-bold font-century-gothic-bold focus:outline-none">
                    </label>
                    @error('fname')
                        <div class="alert alert-danger text-red-500 mt-1">{{ $message }}</div>
                    @enderror
                </div>
                </div>
                <div class="w-full xs:w-full sm:w-full md:w-1/2 lg:w-1/2 xl:w-1/2 px-2">
                <div class="py-4 xs:py-4 sm:py-4 md:py-0 lg:py-0 xl:py-0">
                    <label class="block">
                        <span class="text-green-500">Last Name*</span>
                        <input name="lname" value="{{ old('lname') }}" class="mt-1 block w-full border-green-500 border-b-2 bg-grey-500 py-1 text-green-900 text-3xl font-century-gothic-bold font-century-gothic-bold focus:outline-none">
                    </label>
                    @error('lname')
                        <div class="alert alert-danger text-red-500 mt-1">{{ $message }}</div>
                    @enderror
                </div>
                </div>
            </div>
            </div>



            <div class="px-2 my-4">
            <div class="flex -mx-2">
                <div class="w-full px-2">
                <div class="py-4 xs:py-4 sm:py-4 md:py-0 lg:py-0 xl:py-0">
                    <label class="block">
                        <span class="text-green-500">Email Address*</span>
                        <input type="email" name="email" value="{{ old('email') }}" class="mt-1 block w-full border-green-500 border-b-2 bg-grey-500 py-1 text-green-900 text-3xl font-century-gothic-bold font-century-gothic-bold focus:outline-none">
                    </label>
                    @error('email')
                        <div class="alert alert-danger text-red-500 mt-1">{{ $message }}</div>
                    @enderror
                </div>
                </div>
            </div>
            </div>





            <div class="px-2">
            <div class="flex-wrap xs:flex-wrap sm:flex-wrap md:flex lg:flex xl:flex -mx-2">
                <div class="w-full xs:w-full sm:w-full md:w-1/2 lg:w-1/2 xl:w-1/2 px-2">
                <div class="py-4 xs:py-4 sm:py-4 md:py-0 lg:py-0 xl:py-0">
                    <label class="block">
                        <span class="text-green-500">Contact*</span>
                        <input type="text" name="contact" value="{{ old('contact') }}" class="mt-1 block w-full border-green-500 border-b-2 bg-grey-500 py-1 text-green-900 text-3xl font-century-gothic-bold font-century-gothic-bold focus:outline-none">
                    </label>
                    @error('contact')
                        <div class="alert alert-danger text-red-500 mt-1">{{ $message }}</div>
                    @enderror
                </div>
                </div>
                <div class="w-full xs:w-full sm:w-full md:w-1/2 lg:w-1/2 xl:w-1/2 px-2">
                <div class="py-4 xs:py-4 sm:py-4 md:py-0 lg:py-0 xl:py-0">
                    <label class="block mt-2">
                        <span class="text-green-500">Best time to call*</span>
                        <select name="call" class="form-select mt-1 block w-full rounded-full text-green-900 text-xl font-century-gothic-bold font-century-gothic-bold py-4 focus:outline-none">
                            <option value="{{ old('call') }}">Morning</option>
                            <option value="{{ old('call') }}">Evening</option>
                        </select>
                        </label>
                </div>
                </div>
            </div>
            </div>




        <div class="flex mt-6 mb-4">
        <label class="inline-flex items-center">
            <input type="checkbox" name="agree" class="form-checkbox h-8 w-8 rounded-full focus:outline-none">
            <span class="ml-4 text-xl">I agree to the <span class="underline">privacy policy</span></span>
            </label>
        </div>

        @error('agree')
        <div class="alert alert-danger text-red-500 mb-4">{{ $message }}</div>
        @enderror

        <div class="flex mb-4">
        <div class="w-full px-4">

            <button type="submit" name="contactmailmain" class="shadow bg-blue-500 hover:bg-blue-400 focus:shadow-outline focus:outline-none text-white font-century-gothic-bold py-4 px-4 rounded-full block mx-auto">
                Arrange a Callback
            </button>

        </div>
        </div>

</form>

web.php

Route::get('/', function () {
    return view('home');
});

Route::post('/', function(Request $request ) {
    Mail::send(new ContactMail($request));
    return redirect('/');
});

Route::post('/', function(Request $request ) {
    Mail::send(new ContactMailMain($request));
    return redirect('/');
});

ContactMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Http\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactMail extends Mailable
{
    use Queueable, SerializesModels;

    public $email;

    /**
     * Create a new message instance.
     *
     * @return void
     */`enter code here`
    public function __construct(Request $request)
    {
        $this->email = $request;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build(Request $request)
    {

        $validatedData = $request->validate([
            'name' => 'required|max:255',
            'emailer' => 'required'
        ]);

        var_dump($validatedData);

        return $this->subject('Mail')
                    ->from($this->email->email, $this->email->name)
                    ->to('mail@mail.com')
                    ->view('email.contactmail');
        }

}

ContactMailMain.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Http\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class ContactMailMain extends Mailable
{
    use Queueable, SerializesModels;

    public $emailmain;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Request $request)
    {
        $this->emailmain = $request;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build(Request $request)
    {

        $validatedData1 = $request->validate([
            'fname' => 'required|max:255',
            'lname' => 'required|max:255',
            'email' => 'required',
            'contact' => 'required',
            'agree' => 'accepted'
        ]);

        var_dump($validatedData1);

        return $this->subject('Mail')
                    ->from($this->emailmain->email, $this->emailmain->fname, $this->emailmain->lname)
                    ->to('mail@mail.com')
                    ->view('email.contactmailmain');
    }

}

contactmail.blade.php

<table style="border-collapse: collapse;">
    <tr>
      <th>Name</th>
      <th>Company</th>
      <th>Email</th>
      <th>Answer</th>
    </tr>
    <tr>
      <td style="border: 1px solid black;">&nbsp;{{ $email->name }}&nbsp;</td>
      <td style="border: 1px solid black;">&nbsp;{{ $email->company }}&nbsp;</td>
      <td style="border: 1px solid black;">&nbsp;{{ $email->emailer }}&nbsp;</td>
      <td style="border: 1px solid black;">&nbsp;{{ $email->radio }}&nbsp;</td>
    </tr>
  </table>

contactmailmain.blade.php

<table style="border-collapse: collapse;">
    <tr>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Email</th>
      <th>Contact</th>
      <th>Best time to call</th>
    </tr>
    <tr>
      <td style="border: 1px solid black;">&nbsp;{{ $emailmain->fname }}&nbsp;</td>
      <td style="border: 1px solid black;">&nbsp;{{ $emailmain->lname }}&nbsp;</td>
      <td style="border: 1px solid black;">&nbsp;{{ $emailmain->email }}&nbsp;</td>
      <td style="border: 1px solid black;">&nbsp;{{ $emailmain->contact }}&nbsp;</td>
      <td style="border: 1px solid black;">&nbsp;{{ $emailmain->call }}&nbsp;</td>
    </tr>
  </table>

我希望这两个表单都具有单独的验证,并且都将包含表单数据发送到正确的电子邮件地址。

任何帮助将不胜感激。

杰克。

1 个答案:

答案 0 :(得分:1)

由于Laravel将在第一个匹配项处停止,因此您不能使用相同的方法和URL进行两条路由。

只需为路由使用两个不同的URL并更改action的{​​{1}}属性:

<form>
Route::post('/contact', function(Request $request ) {
    Mail::send(new ContactMail($request));
    return redirect('/');
});

Route::post('/contactMain', function(Request $request ) {
    Mail::send(new ContactMailMain($request));
    return redirect('/');
});