mercredi 2 septembre 2015

Simple contact form not showing validation errors or sending the message

I have a simple contact form in Laravel 5. If I submit it with empty fields, the page reloads but it doesn't show me the error messages of the validation. If I fill in the form and submit, I get this error:

InvalidArgumentException in FileViewFinder.php line 140: View [emails.message] not found.

That's because I don't have such view, I'm not sure what to put in it and Laravel's documentation is not clear on the subject:

The first argument passed to the send method is the name of the view that should be used as the e-mail body.

Not sure what that means.

Routes

Route::get('contact','HomeController@contactus');
Route::post('contact_request', 'HomeController@contactRequest');

HomeController

public function contactus()
    {
        if (Auth::check()) {
            $count = Cart::count();
        } else {
            $count = 0;
        }

        $contact = Contact::all();

        return view('contact')->with('contact', $contact)->with('count', $count);
    }

public function contactRequest() {

        if (Auth::check()) {
            $count = Cart::count();
        } else {
            $count = 0;
        }

        $contact = Contact::all();

        //Get all the data and store it inside Store Variable
        $data = Input::all();

        //Validation rules
        $rules = array (
            'name' => 'required', 
            'email' => 'required|email',  
            'message' => 'required|min:5'
        );

        //Validate data
        $validator = Validator::make ($data, $rules);

        //If everything is correct than run passes.
        if ($validator -> passes()){

            Mail::send('emails.message', $data, function($message) use ($data)
            {
                //$message->from($data['email'] , $data['first_name']); uncomment if using first name and email fields
                $message->from('feedback@gmail.com', 'feedback contact form');
                //email 'To' field: cahnge this to emails that you want to be notified.
                $message->to('feedback@gmail.com', 'John')->cc('feedback@gmail.com')->subject('feedback form submit');

            });
            // Redirect to page
            return view('contact')
                ->with('message', 'Your message has been sent. Thank You!');


            //return View::make('contact');
        }else{
            //return contact form with errors
            return view('contact')
                ->with('error', 'Feedback must contain more than 5 characters. Try Again.')
                ->with('contact', $contact)
                ->with('count', $count);
        }
    }

View

<h1 class='siteh1'>Contact Us</h1> 
<hr class='sitehr'/>
<div class="col-md-6 siteleft">
<h2 class='siteh2'>Contact Info</h2>
<div class='contaddr'><label>Address:</label> {!!$contact[0]['Address']!!}</div>
<div class='contphone'><label>Phone:</label> {!!$contact[0]['Phone1']!!} | {!!$contact[0]['Phone2']!!}</div>
<div class='contemail'><label>E-mail:</label> {!!$contact[0]['Email']!!}</div>
<div class='contfollow'><label>Follow us:</label><p>
                            <a href="{!!$contact[0]['Facebook']!!}"><img src="images/fb.png" alt="Facebook"></a>
                            &nbsp;<a href="{!!$contact[0]['Youtube']!!}"><img src="images/youtube.png" alt="Youtube"></a>
                            &nbsp;<a href="{!!$contact[0]['Skype']!!}"><img src="images/skype.png" alt="Skype"></a>
                            &nbsp;<a href="{!!$contact[0]['Google']!!}"><img src="images/gplus.png" alt="Google Plus"></a>
                        </p></div>
</div>

<div class="col-md-4 col-md-offset-2 col-sm-12 col-xs-12">
<p class='siteright'>
Get in touch with us
</p>
    <div class='contform'>
        {!! Form::open(['action' => 'HomeController@contactRequest', 'METHOD' => 'PUT']) !!}

        <ul class="errors">
            @foreach($errors->all('<li>:message</li>') as $message)

            @endforeach
        </ul>

        <div class="form-group">
            {!! Form:: text('name', '', array('placeholder' => 'Your Name', 'class' => 'form-control', 'id' => 'contname', 'rows' => '4' ))  !!}
        </div>

        <div class="form-group">
            {!! Form:: text('email', '', array('placeholder' => 'Your Email', 'class' => 'form-control', 'id' => 'contemail', 'rows' => '4' )) !!}
        </div>

        <div class="form-group">
            {!! Form:: text('phone', '', array('placeholder' => 'Your Phone', 'class' => 'form-control', 'id' => 'contphone', 'rows' => '4' )) !!}
        </div>

        <div class="form-group">
            {!! Form:: textarea('message', '', array('placeholder' => 'Message', 'class' => 'form-control', 'id' => 'contmessage', 'rows' => '4' )) !!}
        </div>



    </div>
    <div class="modal-footer">
        {!! Form::submit('Submit', array('class' => 'btn btn-primary')) !!}
        {!! Form:: close() !!}
    </div>
</div>



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1JMJ9bP
via IFTTT

Aucun commentaire:

Enregistrer un commentaire