質問

So I am working on an implementation of Ardent in Laravel 4 and I can't seem to sort out why this password confirmation just won't match.

My user controller looks likes this:

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use LaravelBook\Ardent\Ardent;

class User extends Ardent implements UserInterface, RemindableInterface {

public static $rules = array(
  'first_name'            => 'required|min:2|max:80|alpha',
  'last_name'             => 'required|min:2|max:80|alpha',
  'email'                 => 'required|between:3,64|email|unique:users',
  'postal_code'           => 'required|min:5|max:80',
  'password'              => 'required|between:4,20|confirmed',
  'password_confirmation' => 'same:password',
  'timezone_id'           => 'required|numeric',
);

public $autoPurgeRedundantAttributes = true;

Then in the model store action I have the following:

$user = new User(Input::all());
if($user->save()) {
  // does a bunch of stuff
}

But in the end the password confirmation warning is persistant and I can't seem to sort out why? If I take the confirmed option off the password field all works well. I can't seem to get them to understand they are the same. I have included a sample var_dump below that I have tested and confirmed it doesn't pass the validation.

array(10) { 
  ["_token"]=> string(40) "726lKBQgckGuLBxmJZ7Kjq4kzzXqADDqv3ZSnMOE" 
  ["first_name"]=> string(3) "asd" 
  ["last_name"]=> string(3) "asd" 
  ["email"]=> string(14) "asdasd@asd.com" 
  ["postal_code"]=> string(7) "XXX XXX" 
  ["new_game_notification"]=> string(1) "1" 
  ["timezone_id"]=> string(1) "5" 
  ["password"]=> string(6) "asdasd" 
  ["password_confirmation"]=> string(6) "asdasd" 
  ["role"]=> string(7) "regular" 
}

I was doing my own password hashing before save but in an effort to sort this out I have removed that for now.

Any suggestions or guidance / direction is greatly appreciated.

役に立ちましたか?

解決 3

So as it turns out I have found the solution to this.

Ironically, the pasword_confirmation needs to be set as fillable on the model to exist for the validation on Ardent to be able to see it and run the validation.

Fairly simple solution, although not really documented, as why would you put a variable in the fillable array for the model if there isn't a field for it in the database.

他のヒント

Maybe password is hashed before save...

Check doc of ardent:

Route::post('register', function() {
        $rules = array(
            'name'                  => 'required|min:3|max:80|alpha_dash',
            'email'                 => 'required|between:3,64|email|unique:users',
            'password'              => 'required|alpha_num|between:4,8|confirmed',
            'password_confirmation' => 'required|alpha_num|between:4,8'
        );

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->passes()) {
            User::create(array(
                    'name'     => Input::get('name'),
                    'email'    => Input::get('email'),
                    'password' => Hash::make(Input::get('password'))
                ));

            return Redirect::to('/')->with('message', 'Thanks for registering!');
        } else {
            return Redirect::to('/')->withErrors($validator->getMessages());
        }
    }
);

'password' => 'required|alpha_num|between:4,8|confirmed',

'password_confirmation' => 'required|alpha_num|between:4,8',

Not really require "password_confirm" to be in fillable array, I have following rules.

'old_password' => 'required|alphaNum|between:6,16',
'new_password' => 'required|alphaNum|between:6,16|confirmed',
'new_password_confirmation' => 'required|alpha_num|between:4,8'

and in a view I have three fields with same names:

old_password

new_password

new_password_confirmation

and it is working properly for me.

The field under validation must have a matching field of foo_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

Funny enough my form is NOT attached to a model, so your solution did not apply to my case.

In my Laravel 4.2 project, I was having the exact same problem: My password and password confirmation fields matched but I still got an error saying my password_confirmation did not match.

What I found out was that the validation rules "confirmed" and "same" seem to clash. After reading the docs and seeing that what I really needed was "same", I removed "confimred" from password and stepped through with XDebug.

That did the trick for me. Basically, you only need one

Had same validation error on Laravel 4.2. Didn't want to use "same" filter or to set "filled" array with pw_confirmation. Ignored also the explicit way to define a field in db for password. If it's documented, it has to work!

So my solution was:

public static $rules = array(
    'email' => 'required|email|unique:users,email',
    'password' => 'required|min:6|max:20|confirmed'
);

, while in controller:

public function registration() {
    $input = Input::all();

    $validation = Validator::make($input, User::$rules);
    $input['password'] = Hash::make($input['password']);
    // THIS does the trick
    $input['password_confirmation'] = Hash::make($input['password_confirmation']);

    if ($validation->passes())
    {
        User::create($input);

So the confirmation field has just to be Hashed as the password is, before the "passes()" method.

Solution: Just to add variable "password_confirmation" in validation rules

Just like:

$rules = ['password' => 'required|confirmed','password_confirmation'=>''];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top