contact form with Laravel 5.1

Asked

Viewed 1,552 times

2

I need to create a simple contact form with Laravel 5.1, however, as I am more front-end, I would like some idea of what I can do to create this form, some websites even explain how it is done, but it is not with the current version of Laravel, some packagist packages generate a form but also do not explain clearly how to use them.

Someone can give me a light?

  • It’s a fairly broad question. You need to tell us if at least you have it installed and if it’s running.

  • Form is basically HTML, nothing more, nothing less.

  • From Laravel 5 the Form was removed by default from the framework you can use html normally, or install follows the link explaining better Forms & HTML. :)

1 answer

1

To achieve this is very simple

Layout:

{{ Form:: open(array('action' => 'ContactController@getContactUsForm')) }} 

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

@endforeach
</ul>

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



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

Add the following route

Route::post('contact_request','ContactController@getContactUsForm

And the following controller

public function getContactUsForm(){

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

        //Validation rules
        $rules = array (
            //'first_name' => 'required', uncomment if you want to grab this field
            //'email' => 'required|email',  uncomment if you want to grab this field
            'message' => 'required|min:5'
        );

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

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

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

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


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

         }
     }
} 

https://laracasts.com/discuss/channels/general-discussion/creating-a-contactfeedback-form-with-laravel?page=1

  • 1

    In addition to syntax errors (spaces after Form::), your code will not work in version 5.1.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.