Zoul's avatar
Level 5

Contactus form is not working

Hi all,

I don't know where the bug comes from, as it sometimes works and sometimes not, i put return $request in sendContact method before it gets submitted it also sometimes it return data and sometimes can't catch nothing,

in sendContact method

  public function sendContact(ContactUSRequest $request){
        //return $request;
        //print_r($request) ;
        //dump($request);
         try {


        $contact = new ContactUS();
        $details =[
            'name' => $request->name,
            'telephone' => $request->telephone,
            'subject' => $request->subject,
            'message' => $request->message,
            'email' => $request->email,
        ];

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

        //Mail::to('[email protected]')->send(new ContactMail($details)); //use this when in production
    return back()->with(['success'=>trans('front/contactus.Your message has been sent successfully')]);
        } catch (\Throwable $th) {
            return redirect()->back()->with(['error' =>  trans('front/contactus.Your message has been not sent')]);
        }
    }

in blade

Any idea to get this working without bugs ? Many thanks

0 likes
17 replies
tykus's avatar

sometimes works and sometimes not

Can you please describe what sometimes not means; do you get an error message or white screen of death, or something else? What is the data-error attribute for; do you have some client-side validation also? What does the ContactUSRequest FormRequest class look like?

Aside, your controller is a mess; you can get the $rerquest->validated() data from the FormRequest class and save your self multiple lines of unnecessary code? Also, why do you need a try/catch here; what are you expecting will go wrong?

I think your method can work just as well like this:

public function sendContact(ContactUSRequest $request)
{
    $contact = ContactUS::create($request->validated()); // set fillable/guarded on Model

    Mail::to('[email protected]')->send(new ContactMail($request->validated())); 
    return back()->with(['success'=>trans('front/contactus.Your message has been sent successfully')]);
}
1 like
Zoul's avatar
Level 5

Thanks @tykus for your help !

The error i got is The message has already been taken. in red color. when i try to enter a wrong email, phone number or leave empty field, the validation is working correctly

in my ContactUSRequest

The try/catch i uncommented it when i posted here, as you advised no need for it and i just removed it

tykus's avatar
tykus
Best Answer
Level 104

@Zoul why would the message need to be unique??? That seems like a ridiculous rule to impose

experimentor's avatar

@Zoul This validation rule

'message' =>  ['required', 'string', Rule::unique('contactus','message')], 

Is saying that the message has to be unique.

Assume customer1 posts the contact form with message "I am interested in this" . They will get success response.

After sometime, if customer2 posts the same message: "I am interested in this". They will get error "Message already taken" since your rule says the message has to be unique.

How will customer2 know what customer1 wrote on your website? Please remove this rule. Change it to:

'message' =>  ['required', 'string']

That should be enough.

1 like
Vishal007's avatar

Define Log Facecase using this

use Exception; // if need use it
use Illuminate\Support\Facades\Log;
public function sendContact(Request $request){
        // use this dd
        dd($request->all);
         try {
        $contact = new ContactUS();
        $contact->name = $request->name;
        $contact->email = $request->email;
        $contact->telephone = $request->telephone;
        $contact->message = $request->message;
        $contact->subject = $request->subject;
        $contact->save();
//check into log
Log::info('data store successfully into database : '.$contact);
//dd here
dd($contact);
        //Mail::to('[email protected]')->send(new ContactMail($details)); //use this when in production
    return back()->with(['success'=>trans('front/contactus.Your message has been sent successfully')]);
        } catch (\Exception $e) {
Log::info('data store successfully into database : '.$e->getMessage);
            return redirect()->back()->with(['error' =>  trans('front/contactus.Your message has been not sent')]);
        }
    }

also check all with dd method to data is posted or not id still issue then please reply

1 like
Vishal007's avatar

Also Use Validator Using this

use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required',
            // all require parameter add here
        ]);

        if ($validator->fails()) {
            Log::info('validation failed : '.$validator->errors());
            return response()->json([
                'error' => true,
                'message' => $validator->errors()
            ], 422);
        }
1 like
Zoul's avatar
Level 5

Thanks @Vishal007 for your help!

I used the code you suggested but neither dd($request->all); or dd($contact); got triggered i see only the error message The message has already been taken. down the form

tykus's avatar

@Zoul requiring a unique message, why? In any case this is why your form does not submit successfully, it is simply a validation error like any other

Vishal007's avatar

@Zoul check into database migration the message field you can add unique but this is nothig to need message is not unique if you need then into validation use regex for message field or if message is unique then into validation rule add unique into message field so not get any error and if already then inform first

// also try it
'message' => 'required|message|unique:contactus,message'
1 like
Jsanwo64's avatar

also @zoul why is your form having

enctype="multipart/form-data"

Is there a hidden file upload?

1 like
Zoul's avatar
Level 5

Thanks @jsanwo64, my form is messed up, so i removed it since no need for a file here

Jsanwo64's avatar
The error i got is The message has already been taken. in red color. when i try to enter a wrong email, phone number or leave empty field, the validation is working correctly

in my ContactUSRequest

is due to this

            'message' =>  ['required', 'string', Rule::unique('contactus','message')], 
Jsanwo64's avatar

Since it is just a contact form that submits to the DB and sends a mail it should not complicated

try this

N.B: The try and catch is not not necessary, unless you have done something to expect it returning an error.

1 like
Snapey's avatar

write validated data to the contact model

pass the contact model to the mailable rather than creating separate array

recognise validation errors rather than errors in your controller

1 like
Zoul's avatar
Level 5

I remember going through dubplicated message issue when a client submit it once but i got 2, so i implemented this but if we take @experimentor example, it won't just work. Thank alot all of you for your support i really appreciate it

Please or to participate in this conversation.