Search code examples
phplaravellaravel-validationlaravel-mail

Multiple Forms Validation Mailable with Laravel 5.8


I have a Laravel page with Multiple Forms, that need to send and also be validated. Every time I submit 'Form2' it submits and validates 'form1'

I've tried wrapping it in this

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

Either form works if I remove the other ones route the other one works with correct validation.

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

But that doesn't seem to do anything.

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>

I expect both forms to have separate validation and also both send to the correct email address with the form data.

Any help would be much appreciated.

Jake.


Solution

  • You can't have two routes with the same method and URL since Laravel will stop at the first match.

    Just use two different URLs for the routes and change the action property of the <form>s:

    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('/');
    });
    
    <form method="POST" action="{{ url('/contact') }}">