I am trying to understand why posted values persist in a session, even after a browser refresh. I would have thought that if a request is reset by a refresh (F5), that the form would be reset back to its original state.
When I navigate to http://localhost/register, the form is shown in its initial state. When I try to submit with errors, the validation errors appear correctly. If I try to refresh (F5) the form errors persist. If I navigate away from http://localhost/register and the return, the errors still appear as though I just submitted an erroneous form. When I submit a valid form, the user is registered correctly in the database and I get the "registration_success" session variable that shows that the request was completed. If I try to refresh the browser, the "registration_success" session variable persists and I cannot make it disappear with browser refresh or navigation away from and back to the page. So if another user tries to register, the session persists and the registration form cannot be rendered (until the session expires at the preset 30 minutes). I would have thought that the get-route would have been used on a browser refresh, thereby resetting the form.
Can someone please explain to me what is happening here, if this is what Laravel is designed to do and how I can get the form to reset with a refresh?
Here are my routes:
Route::get('register', array(
'uses' => 'UserController@register',
'as' => 'users.register'
));
Route::post('register', array(
'uses' => 'UserController@create',
'as' => 'users.create'
));
Here are my relevant UserController functions:
public function register()
{
return View::make('users.register');
}
public function create()
{
$input = Input::all();
$rules = array(
'email'=> 'email|unique:users|required',
'first_name'=>'required',
'last_name'=>'required',
'password'=>'min:6|confirmed|required',
'password_confirmation'=>'same:password|required',
);
$validator = Validator::make($input,$rules);
if($validator->fails()){
$messages = $validator->messages();
return Redirect::to('register')->withInput()->withErrors($validator);
}else{
$user = Sentry::register(array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'first_name' => Input::get('first_name'),
'last_name' => Input::get('last_name')
));
return Redirect::back()->with('registration_success', 'Registration Complete!');
}
}
Here is the relevant form:
@if (Session::has('registration_success'))
<div>
<h3>{{ Session::get('registration_success') }}</h3>
</div>
@else
@if ($errors->any())
<ul>
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
@endif
<div class="row">
<div class="col-md-6 col-md-offset-3">
{{ Form::open(array('action' => 'UserController@create')) }}
<div class="form-group <?php if($errors->has('first_name')){echo "has-error";}?>">
{{ Form::label('first_name', 'First Name:') }}
{{ Form::text('first_name', '', array('placeholder'=>'First Name', 'class'=>$errors->has('first_name') ? 'form-control has-error' : 'form-control')) }}
</div>
<div class="form-group <?php if($errors->has('last_name')){echo "has-error";}?>">
{{ Form::label('last_name', 'Last Name:') }}
{{ Form::text('last_name', '', array('placeholder'=>'Last Name', 'class'=>$errors->has('last_name') ? 'form-control has-error' : 'form-control')) }}
</div>
<div class="form-group <?php if($errors->has('email')){echo "has-error";}?>">
{{ Form::label('email', 'Email:') }}
{{ Form::text('email', '', array('placeholder'=>'Email', 'class'=>$errors->has('email') ? 'form-control has-error' : 'form-control')) }}
</div>
<div class="form-group <?php if($errors->has('password')){echo "has-error";}?>">
{{ Form::label('password', 'Password:') }}
{{ Form::password('password', array('placeholder'=>'Password', 'class'=>$errors->has('password') ? 'form-control has-error' : 'form-control')) }}
</div>
<div class="form-group <?php if($errors->has('password_confirmation')){echo "has-error";}?>">
{{ Form::label('password', 'Confirm Password:') }}
{{ Form::password('password_confirmation', array('placeholder'=>'Password (again)', 'class'=>$errors->has('password_confirmation') ? 'form-control has-error' : 'form-control')) }}
</div>
<div class="form-group">
{{ Form::submit('Register',array('class'=>'btn btn-success')) }}
</div>
{{ Form::close() }}
</div>
</div>
@endif
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community