Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

brakkar's avatar

old values not displaying

Hello, I have this code in a blade template:

Email:<br>
<input type="email" name="email" value="{{ old('email') }}"><br/>
@if ($errors->first('email'))
    <li>
        {{ $errors->first('email')  }}
    </li>
@endif

error display works fine, but not the 'old' things.

The controler is very simple, do I need to do something special to enable old values:

public function store() { # Validate $this->validate(request(), [ 'username' => 'required|min:2', 'email' => 'required|email', 'password' => 'required', ]);

    # Create
    $user = User::create(request(['username', 'email', 'password']));

    # Authenticate
    //auth()->login($user);

    # redirect
    return redirect()->home();
}
0 likes
49 replies
Jaytee's avatar

So nothing shows up in the inputs if validation fails?

P.S: You're not hashing your password when creating a user. Never save the password as plain text.

User::create([
    'username' => request('username'),
    'email' => request('email'),
    'password' => bcrypt(request('password'))
]);
1 like
brakkar's avatar

Nothing : value="" for all fields.

Thanks for password tip, this is just for testing, i'm far, far from production.

khaledSMQ's avatar

have you tried to use Request validation class, and try to change it to {!! old('email') !!}

Jaytee's avatar

Doesn't matter whether you're far from production. Good habit to get into and a necessary step. If you use the Auth::attempt() to login a user then it's gonna hash it behind the scenes and compare it. Then it'll fail.

Wanna post your whole form and the whole class for registering a user please.

brakkar's avatar

Sure thanks, here is the form

<form method="post" action="/user/register">
    {{csrf_field() }}
    username:<br>
    <input type="text" name="username" value="{{ old('username') }}"><br/>
    @if ($errors->first('username'))
        <li>
            {{ $errors->first('username')  }}
        </li>
    @endif
    <br><br>


    Email:<br>
    <input type="email" name="email" value="{{ old('email') }}"><br/>
    @if ($errors->first('email'))
        <li>
            {{ $errors->first('email')  }}
        </li>
    @endif
    <br><br>

    password:<br>
    <input type="password" name="password" value="{{ old('password') }}">
    @if ($errors->first('password'))
        <li>
            {{ $errors->first('password')  }}
        </li>
    @endif
    <br><br>
    <button type="submit"> SUBMIT FORM</button>
</form>

And here is the controller:

class UsersController extends Controller
{
    public function login()
    {
        return view( 'users.login' );
    }


    #------------------------------------------------------------------------------------
    # Registration
    #------------------------------------------------------------------------------------
    public function create()
    {
        return view( 'users.registration' );
    }


    public function store()
    {
        # Validate
        $this->validate(request(), [
            'username' => 'required|min:2',
            'email' => 'required|email',
            'password' => 'required',
        ]);

        # Create
        $user = User::create(request(['username', 'email', 'password']));

        # Authenticate
        //auth()->login($user);

        # redirect
        return redirect()->home();
    }

}

and the routes

Route::get( '/user/register', 'users\UsersController@create' );
Route::post( '/user/register', 'users\UsersController@store' );
Jaytee's avatar

Just to double check. Can you post your routes too for the registration. Both the get and post routes.

brakkar's avatar

Here are all the current routes:

Route::get( '/', 'base\BaseController@index' )->name('home');


#------------------------------------------------------------------------------------
# Users
#------------------------------------------------------------------------------------
Route::get('/user/login', 'users\UsersController@login');

Route::get( '/user/register', 'users\UsersController@create' );
Route::post( '/user/register', 'users\UsersController@store' );
khaledSMQ's avatar

try

    $v = Validator::make($data, [
             'username' => 'required|min:2',
            'email' => 'required|email',
            'password' => 'required',
        ]);

    if ($v->fails()) {
            return redirect('your route here')
                        ->withErrors($validator)
                        ->withInput();
        }


3 likes
imechemi's avatar

@khaledSMQ I missed withInput() . It happened since I wrote custom validation using closure. Thanks

brakkar's avatar

Thanks Khaled but I need to do it manually. Also in your example, $data cause an error...

khaledSMQ's avatar

yes you need to replace data with your request like this

$data = $request->only('username', ..... what ever data you want)
Jaytee's avatar

Alright paste this in your view:

@if ($errors->count())
    {{ dd(request()) }}
@endif

Make the validation fail and then you'll be presented with the Request instance.

Click on session > attributes > _old_input and see if your values are in there.

khaledSMQ's avatar

here is full code

   public function store(Request $request)
    {
         $this->validator($request->all())->validate();
 
        //->validate(); it will take care to throw exception if the validate fails

        # Create
        $user = $this->create($request->all()))
        # Authenticate
        //auth()->login($user);

        # redirect
        return redirect()->home();
    }


protected function create(array $data)
    {
        return User::create([
            'name' => $data['username'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']), // always make sure not plain password in your database
        ]);
    }

 protected function validator(array $data)
    {
        return Validator::make($data, [
           'username' => 'required|min:2',
            'email' => 'required|email',
            'password' => 'required',
        ]);
    }
brakkar's avatar

Jaytee old values are all 'null' !!

brakkar's avatar

Khaled, thanks for code:

Type error: Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, object given, called in /Users/ME/Documents/Apps_And_Sites/PHP_Apps/rib/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php on line 221

Jaytee's avatar

Alright, what have you done since starting this project? Have you made use of session anywhere or changed any settings in config?

brakkar's avatar

Jaytee, the project is really new i've been following tutorials and haven't touched any setting except the mysql ones. I did a few migration, modified a bit the User model. I didn't changed session or touched anything about session. Maybe they are disabled ?

Jaytee's avatar

Yeah but the weird thing is, you're getting the errors just not the old data and they're both through sessions.

Try running a few artisan commands to clear the application cache and such:

composer dump-autoload
php artisan cache:clear
php artisan route:clear

Also, just try removing the form entirely and start fresh and then see what happens. I'm quite baffled actually

SaeedPrez's avatar

@Jaytee was on the right track.. try add this to the bottom/top of your blade template where you have the form, submit the form.. then paste the result (code or screenshot) here..

<form>
    <!-- your form html -->
</form>
{{ dump(session()->all(), old()) }}

PS. Make sure to open up all arrays in the result so we can see everything that is return.

Jaytee's avatar

@SaeedPrez I knew you'd come to the rescue ;) I was actually going to tag you. It's baffling me that the errors are appearing but not the old data.

1 like
brakkar's avatar
array:5 [▼
  "_token" => "IIZcvyxqKwI7WrQ6YKXEqq6NAszLNIps7UvEjAYZ"
  "_previous" => array:1 [▼
    "url" => "http://localhost:8000/user/register"
  ]
  "_flash" => array:2 [▼
    "old" => array:2 [▼
      0 => "_old_input"
      1 => "errors"
    ]
    "new" => []
  ]
  "_old_input" => array:4 [▼
    "_token" => "IIZcvyxqKwI7WrQ6YKXEqq6NAszLNIps7UvEjAYZ"
    "username" => null
    "email" => null
    "password" => null
  ]
  "errors" => ViewErrorBag {#155 ▼
    #bags: array:1 [▼
      "default" => MessageBag {#156 ▼
        #messages: array:3 [▼
          "username" => array:1 [▼
            0 => "The username field is required."
          ]
          "email" => array:1 [▼
            0 => "The email field is required."
          ]
          "password" => array:1 [▼
            0 => "The password field is required."
          ]
        ]
        #format: ":message"
      }
    ]
  }
]
array:4 [▼
  "_token" => "IIZcvyxqKwI7WrQ6YKXEqq6NAszLNIps7UvEjAYZ"
  "username" => null
  "email" => null
  "password" => null
]
SaeedPrez's avatar

@brakkar it seems your form is not submitting the values.. Can you show your whole template where you have the form?

@Jaytee hehe, my guess is there is some HTML mistake..

brakkar's avatar

Sure, here is registration.blade.php

REGISTRATION
<br/>


{{--@if ($errors->count())--}}
    {{--{{ dd(request()) }}--}}
{{--@endif--}}

<form method="post" action="/user/register">
    {{csrf_field() }}
    username:<br>
    <input type="text" name="username" value="{{ old('username') }}"><br/>
    @if ($errors->first('username'))
        <li>
            {{ $errors->first('username')  }}
        </li>
    @endif
    <br><br>


    Email:<br>
    <input type="email" name="email" value="{{ old('email') }}"><br/>
    @if ($errors->first('email'))
        <li>
            {{ $errors->first('email')  }}
        </li>
    @endif
    <br><br>

    password:<br>
    <input type="password" name="password" value="{{ old('password') }}">
    @if ($errors->first('password'))
        <li>
            {{ $errors->first('password')  }}
        </li>
    @endif
    <br><br>
    <button type="submit"> SUBMIT FORM</button>
</form>
{{ dump(session()->all(), old()) }}

Jaytee's avatar

I'm pretty sure it won't make a difference but try changing your post route to just /user instead of /user/register

Jaytee's avatar

@SaeedPrez That's what i was thinking. I reviewed the form so many times and couldn't find anything.

brakkar's avatar

Jaytee, indeed changing the post route didn't resolve the problem.

Jaytee's avatar

Okay start from scratch: as for your action on your form, change it to this:

{{ url('/user/register') }}

See what happens then.

SaeedPrez's avatar

@brakkar your HTML seems to be just fine, try add this line to top of your store() method and paste the results after filling out the form and submitting..

public function store() {
    dd(request()->all());

    // your other code
}
Next

Please or to participate in this conversation.