Home > Enterprise >  Laravel beginner need help solving error 419
Laravel beginner need help solving error 419

Time:05-09

I'm currently working on my first web project with Laravel. The app will has a very simple task, the user puts in a date and my website will check if that date is in a set timeframe. So its first getting user input, secound check if the input date is in between 2 other dates and last print a massage out on the view when depending on the outcome of step 2. Sadly every time i press the submit-button which should give me some output i always get an 419 error, I hope someone can tell me what I am doing wrong.

Here is my view named welcome.blade.php

<!DOCTYPE html>
<html>  
<body>
      
    <b> Klausuretermin auswählen
    </b>
      
    <br>
    <form method="POST" action="/dateCalc">
      <input type="date" id="date" value="date">
    
    <br>
    <input type="text" name="klausurname" value="klausurname">
    <br>
    <button type="submit" name="submit">Submit</button> 
    </form>
    <br>
    @if (isset($result))
        <p>{{ $result }}</p>
    @endif
</body>
  
</html>        

Here is my model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class DateCalc extends Model
{
    public function checkDateBetween( $toCheck){
        //still needs logic want to print out input to test the rest
        return $toCheck;
    }
}

here my controller:

<?php

namespace App\Http\Controllers;

use App\DataCalc;
use Illuminate\View\View;
use Illuminate\Http\Request;

class DateController extends Controller
{
    public function index(): View{
        return view('welcome');
    }    
    public function checkDate(DateCalc $dateCalc): View{
        $date = "OK";
        
        return view('welcome', ['result' => $$date]);
    }
}

and at last my routes:

Route::get('/dateCalc','DateController@index');
Route::post('/dateCalc', 'DateController@checkDate');

CodePudding user response:

you have to add csrf protection in your form

<!DOCTYPE html>
<html>  
<body>
      
    <b> Klausuretermin auswählen
    </b>
      
    <br>
    <form method="POST" action="/dateCalc">
      @csrf
      <input type="date" id="date" value="date">
    
    <br>
    <input type="text" name="klausurname" value="klausurname">
    <br>
    <button type="submit" name="submit">Submit</button> 
    </form>
    <br>
    @if (isset($result))
        <p>{{ $result }}</p>
    @endif
</body>

you can read more in documentation https://laravel.com/docs/9.x/csrf#main-content

CodePudding user response:

Please add @csrf token in your form.

Example :

<form method="POST">
  @csrf  // CSRF 
</form>
  • Related