拉拉维尔 4 - 热情 - 密码确认不匹配


Laravel 4 - Ardent - The password confirmation does not match

所以我正在Laravel 4中实现Ardent,我似乎无法弄清楚为什么这个密码确认不匹配。

我的用户控制器如下所示:

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;

然后在模型存储操作中,我有以下内容:

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

但最终密码确认警告仍然存在,我似乎无法弄清楚为什么? 如果我从密码字段中删除已确认的选项,一切正常。 我似乎无法让他们明白他们是相同的。 我在下面包含一个示例var_dump,我已经测试并确认它没有通过验证。

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" 
}

我在保存之前正在做自己的密码哈希,但为了解决这个问题,我现在已经删除了它。

任何建议或指导/方向将不胜感激。

也许密码在保存之前被散列...

检查热心的文档:

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());
        }
    }
);

"密码" => "必需|alpha_num|介于:4,8|已确认",

'password_confirmation' => '必需|alpha_num|之间:4,8',

实际上并不要求"password_confirm"在可填充数组中,我有以下规则。

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

在一个视图中,我有三个同名的字段:

old_password

new_password

new_password_confirmation

它对我来说工作正常。

待验证的字段必须具有匹配字段 foo_confirmation 。例如,如果验证中的字段是密码,则输入中必须存在匹配的password_confirmation字段。

事实证明,

我已经找到了解决方案。

具有讽刺意味的是,pasword_confirmation需要在模型上设置为可填充,以便 Ardent 上的验证能够看到它并运行验证。

相当简单的解决方案,虽然没有真正记录,因为如果数据库中没有变量的字段,为什么要在模型的可填充数组中放置一个变量。

有趣的是

,我的表单没有附加到模型,因此您的解决方案不适用于我的情况。

在我的Laravel 4.2项目中,我遇到了完全相同的问题:我的密码和密码确认字段匹配,但我仍然收到一个错误,说我的password_confirmation不匹配。

我发现"确认"和"相同"的验证规则似乎冲突。在阅读文档并看到我真正需要的是"相同"之后,我从密码中删除了"confimred"并使用 XDebug 逐步完成。

这对我来说起到了作用。基本上,你只需要一个

在 Laravel 4.2 上也有相同的验证错误。不想使用"相同"过滤器或使用pw_confirmation设置"填充"数组。还忽略了在 db 中为密码定义字段的显式方法。如果它被记录下来,它必须工作!

所以我的解决方案是:

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

,在控制器中:

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);

因此,确认字段必须像密码一样进行哈希处理,在"passes()"方法之前。

解决方案:只需在验证规则中添加变量"password_confirmation"

就像:

$rules = ['password' => 'required|confirmed','password_confirmation'=>''];