Goldensquare
Goldensquare

Reputation: 331

what is the correct way of form validation in laravel?

I have just created the form and action is PagesController@check and the validation is as follows:

@extends('layout')

@section('content')

<div class = "container">
    {!! Form::open(['action' => 'PagesController@check' , 'method' => 'POST']) !!}

        <div class = "form-group">
            {{ Form::label('country','Country')}}
            {{ Form::text('country','', ['class' => 'form-control' , 'placeholder' => ''])}}
        </div>  

        <div class = "form-group">
            {{ Form::label('age','Age')}}
            {{ Form::number('age','', ['class' => 'form-control' , 'placeholder' => ''])}}
        </div>  

        <div class = "form-group">
            {{ Form::label('marks','Marks')}}
            {{ Form::number('marks','', ['class' => 'form-control' , 'placeholder' => ''])}}
        </div>  

        <div class = "form-group">
            {{ Form::label('description','Description')}}
            {{ Form::textarea('description','', ['class' => 'form-control' , 'placeholder' => ''])}}
        </div>  

        {{ Form::submit('Submit' , ['class' => 'btn btn-primary'])}}

    {!! Form::close() !!}
</div>
@endsection

And the check() method in the PagesController is like this:

    public function check(Request $request){
        $this->validate($request, [
            'country' => 'required',
            'age' => 'required',
            'marks' => 'required',
            'description' => 'required'

            ]);
        return 123;

   }

Why is it then it is throwing the following error:

(2/2) ErrorException
Action App\Http\Controllers\PagesController@check not defined. (View: C:\wamp64\bin\apache\apache2.4.23\htdocs\website\resources\views\profiles.blade.php)

Here is the whole PagesController controller:

class PagesController extends Controller
{
    public function home() {
        return view('welcome');
    }

    public function about() {
        $title = 'This is the about page';
        return view('about')->with('title',$title);
    }

    public function show() {
        $yomads = person::all();
        return view('show')->with('yomads',$yomads);
    }

    public function profiles(){
       return view('profiles');
    }

    public function check(Request $request){
        $this->validate($request, [
            'country' => 'required',
            'age' => 'required',
            'marks' => 'required',
            'description' => 'required'

            ]);
        return 123;

   }
}

Upvotes: 1

Views: 391

Answers (1)

Javier Arias
Javier Arias

Reputation: 2478

The error most likely has to do with the route (or lack of it) in app/Http/routes.php - check that it is properly defined there.

Furthermore, it is good practice to create custom request classes. Have a look at Form Request Validation

These can be generated with artisan:

php artisan make:request Profile

Then use it, as you were using the standard request:

public function check(ProfileRequest $request) {
[...]

Upvotes: 1

Related Questions