vivek321
vivek321

Reputation: 199

form validation not working in laravel

I am working on laravel 5.3.30 and created a profile page with form and try to validate the data when the form is submitted but I am not getting any errors after submitting the form, its just refresh the page.

Route File:

Route::get('/', function () {
    return view('main');
});

Auth::routes();

Route::get('/home', 'HomeController@index');

Route::get('logout', '\App\Http\Controllers\Auth\LoginController@logout');
Route::resource('profile','ProfileController');

Profile Form:

{!! Form::open(array('route'=>'profile.store')) !!}
    <div class="form-group">
    {{Form::label('first_name','Firstname')}}<span class="required">*</span>
 {{Form::text('first_name',null,['class'=>'form-control','placeholder'=>'Enter Firstname'])}}
        </div>
        <div class="form-group">
                                              {{Form::label('last_name','Lastname')}}<span class="required">*</span>
    {{Form::text('last_name',null,['class'=>'form-control','placeholder'=>'Enter Lastname'])}}
     </div>
     {{Form::submit('Create',array('class'=>'form-submit btn btn-success btn-block btn-lg'))}}

{!! Form::close() !!}

Validation in Profile Controller:

public function store(Request $request)
    {
       $this->validate($request,array(
            'first_name'=>'required|max:255',
            'last_name'=>'required|max:255'
   ));
   }

When I submit the form without filling anything, it just refresh the page and does not show any errors. Please suggest something. Thanks in advance.

Upvotes: 0

Views: 2966

Answers (1)

Jazzzzzz
Jazzzzzz

Reputation: 1633

It looks like you forget to send error from controller and print in view.

here is controller code should look like

public function store(Request $request)
{
    $this->validate($request,array(
            'first_name'=>'required|max:255',
            'last_name'=>'required|max:255'
    ));

    // include this line incase of validation error
    return $validator->errors()->all();
}

You need to print error in view in order to know user

@if (count($errors) > 0)
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

Upvotes: 1

Related Questions