Leff
Leff

Reputation: 1360

Laravel 5 - requests validation not returning messages

I am trying to post from a simple contact form, I have created also a simple validation rules for it:

<?php

namespace App\Http\Requests\Contact;

use Illuminate\Foundation\Http\FormRequest;

class ContactCreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required|email',
            'message' => 'max:500',
        ];
    }
}

The is the form that I am posting from:

@extends('public.layouts.master')
@section('content')
<div class="container contact-form">
    <div class="row">
      <div class="col-md-6 col-md-offset-3">
        @if (session('status'))
            <div class="alert alert-warning alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              {{ session('status') }}
            </div>
        @endif
        <div class="well well-sm">
          <form autocomplete="off" class="form" method="POST" action="{{ url('/contact') }}">
          {{ csrf_field() }}
          <fieldset>
            <legend class="text-center">Contact us</legend>

            <!-- Name input-->
            <div class="form-group">
              <label class="col-md-3 control-label" for="name">Name</label>
              <div class="col-md-9">
                <input id="name" name="name" type="text" placeholder="Your name" class="form-control">
              </div>
            </div>

            <!-- Email input-->
            <div class="form-group">
              <label class="col-md-3 control-label" for="email">Your E-mail</label>
              <div class="col-md-9">
                <input id="email" name="email" type="email" placeholder="Your email" class="form-control" required>
                @if ($errors->has('email'))
                    <span class="help-block">
                        <strong>{{ $errors->first('email') }}</strong>
                    </span>
                @endif
              </div>
            </div>

            <!-- Message body -->
            <div class="form-group">
              <label class="col-md-3 control-label" for="message">Your message</label>
              <div class="col-md-9">
                <textarea class="form-control" id="message" name="message" placeholder="Please enter your message here..." rows="5"></textarea>
                @if ($errors->has('message'))
                    <span class="help-block">
                        <strong>{{ $errors->first('message') }}</strong>
                    </span>
                @endif
              </div>
            </div>

            <!-- Form actions -->
            <div class="form-group">
              <div class="col-md-12 text-right">
                <button type="submit" class="btn btn-primary">Submit</button>
              </div>
            </div>
          </fieldset>
          </form>
        </div>
      </div>
    </div>
</div>
@endsection

And this is the controller that I am posting to:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\Contact\ContactCreateRequest;
use App\Events\MessageSent;

class ContactController extends Controller
{
    /**
     * Show the first page.
     *
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        return view('contact.contact-form');
    }

    public function create(ContactCreateRequest $request)
    {
        event(new MessageSent($request->message, $request->email));

        return redirect()->back()->with('status', 'The message was succesfully sent to us, we will get back to you inside of 24 hours');
    }
}

But, when I am trying to post anything I just get an error:

Forbidden

Why is that, and why I don't see any error messages in the contact form if the validation doesn't go through?

Upvotes: 0

Views: 57

Answers (1)

Rodrigo Cabrera
Rodrigo Cabrera

Reputation: 164

If you're not logged in, you should try to change this in your Request file

 public function authorize()
{
    return true;
}

Upvotes: 1

Related Questions