0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Hey Jyadip, thanks for the solution but I am running other project of same version and in that project it is working very well without any need to add this code. I am still searching how it works
I forgot to add messages block which displays error. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.