0

I am using Laravel 5 and I want to use input validation for my form. But my validation is not working. Do you know why?
Here is the view:

<div class="form-group"><br>

        <label for="jenis_surat" class="control-label col-sm-2">Jenis Surat</label>
        <div class="col-sm-7">

          <script type="text/javascript">
        @if($errors->any())
            <div class="alert alert-danger">
                @foreach($errors->all() as $error)
                    <p>{{ $error }}</p>
                @endforeach
            </div>
        @endif
      </script>

            <input class="form-control" type="text" name="jenis_surat" id="jenis_surat">
        </div>  
        <div class="col-md-1">
          <input class="form-control" type="hidden" name="_token" value="{{ Session::token() }}">
          <a class="btn btn-success" data-toggle="modal" data-target="#konfirmasi_submit_jenis_surat">Submit</a>   
        </div>
    </div>

and this is the controller:

public function tambahjenissurat(Request $request)
{
    //input validation
    $this->validate($request, [
        'jenis_surat' => 'required'
    ]);

    $jenis_surat = $request['jenis_surat'];

    $jenissurat = new JenisSurat();
    $jenissurat->jenis_surat = $jenis_surat;

    $jenissurat->save();

    return redirect()->route('jenissurat');
}
hendraspt
  • 959
  • 3
  • 19
  • 42
  • Check the solution here: http://stackoverflow.com/a/34421349/2594162 – Sovon Apr 29 '16 at 04:24
  • I don't understand "['middleware' => ['web']]" in Route::group(['middleware' => ['web']], function () { // Add your routes here});. I have tried to copy it all but I have got an error – hendraspt Apr 29 '16 at 04:40
  • Exactly which Laravel version are you using? In Laravel 5.2.0 to 5.2.26, all the routes should be wrapped inside 'web' middleware. This 'web' middleware instantiate the session. But in Laravel 5.2.27, laravel automatically add this 'web' middleware, so no need to wrap the routes. – Sovon Apr 29 '16 at 04:45

1 Answers1

1

Could you please try like this? 1. Import the validator using use Validator; 2. Change the code that validates the rule as given below

$rules = array(
        'jenis_surat' => 'required'
    );
    $validator = Validator::make(Input::all(), $rules);
    if ($validator->fails()) {
        return redirect()->back()
            ->withErrors($validator);
    }
Praveesh
  • 1,257
  • 1
  • 10
  • 23