在laravel / ardent中用密码编辑用户模型的预期方法是什么?我的问题是,在正确验证用户输入之前,我不想从db加载实际的用户模型.当我将密码字段留空时验证显然失败,因为需要密码.这是我目前的编辑后动作:
public function postEdit($id)
{
// ardent autohydrates this model
$newUser = new User;
// validation fails
if(!$newUser->validate())
return Redirect::action('UsersController@getEdit', $id)
->with('error', Lang::get('Bitte Eingabe überprüfen'))
->withErrors($newUser->errors())
->withInput(Input::except('password'));
// load model from db
$exUser = User::find($id);
if(!$exUser->exists)
return Response::make('No such user', 500);
// save model, ardent autohydrates again?
if($exUser->save())
return Redirect::action('UsersController@getShow', $id)
->with('success', Lang::get('?nderungen gespeichert'));
else
return Redirect::action('UsersController@getEdit', $id)
->with('error', Lang::get('Bitte Eingabe überprüfen'))
->withErrors($newUser->errors())
->withInput(Input::except('password'));
}
这似乎是一个非常多的代码(它不工作),我无法找到这种情况的例子 解决方法: 好的,我自己解决了,因为这不是一个非常活跃的话题.
问题是结合了ardents自动水合功能和保留旧密码的独特要求,如果没有给出新密码.因为在validate()和save()上进行了自动水合,所以也无法阻止自动水合空密码.首先,我尝试更改Input数组并使用旧密码覆盖它,但之后我只是关闭了用户模型的自动水合:
class User extends Ardent implements UserInterface, RemindableInterface {
public $forceEntityHydrationFromInput = false;
public $autoHydrateEntityFromInput = false;
这是POST上的编辑操作:
public function postEdit($id)
{
// manually insert the input
$user = new User(Input::all());
// validate the user with special rules (password not required)
if($user->validate(User::$updateRules)) {
// get user from database and fill with input except password
$user = User::find($id);
$user->fill(Input::except('password'));
// fill in password if it is not empty
// will flag the pw as dirty, which will trigger rehashing on save()
if(!empty(Input::get('password')))
$user->password = Input::get('password');
if($user->save())
return Redirect::action('UsersController@getIndex')
->with('success', Lang::get('?nderungen gespeichert'));
}
return Redirect::action('UsersController@getEdit', $id)
->with('error', Lang::get('Bitte Eingaben überprüfen'))
->withErrors($user->errors())
->withInput(Input::except('password'));
}
(编辑:北几岛)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|