Laravel/Ardent/用户模型编辑+保存 [英] Laravel/Ardent/User model editing + saving
问题描述
在laravel/ardent中用密码编辑用户模型的预期方法是什么?我的问题是,我不想在正确验证用户输入之前从数据库加载实际的用户模型.当我将密码字段保留为空时,验证显然会失败,因为需要密码.这是我当前的后期编辑操作:
what is the intended way to edit a user model with password in laravel/ardent? My problem is, that I do not want to load the actual user model from the db before the user input is validated correctly. Validation obviously fails when I leave the password field empty, because a password is required. This is my current post-edit-action:
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'));
}
这似乎是很多代码(+无法正常工作),我无法找到这种情况的示例
this seems like an awful lot of code (+ it is not working), I was unable to find an example for this situation
推荐答案
好吧,我自己解决了这个问题,因为这不是一个非常活跃的话题.
Ok, solved it myself, seeing as this is not a very active topic.
问题在于结合了热情的自动补水功能和唯一的保留旧密码的要求(如果没有提供新密码的话).因为在validate()
和save()
上热切自动补水,所以也没有办法防止空密码自动补水.首先,我尝试更改Input数组并用旧密码覆盖它,但是随后我只是为用户模型关闭了自动补水功能:
The problem was combining ardents autohydration feature and the unique requirement to retain the old password, if no new one is given. Because ardent autohydrates on validate()
AND save()
, there was no way to prevent autohydrating empty passwords too. First, I tried to change the Input array and overwrite it with the old password, but then I simply turned off the autohydration for the user model:
class User extends Ardent implements UserInterface, RemindableInterface {
public $forceEntityHydrationFromInput = false;
public $autoHydrateEntityFromInput = false;
这是对POST的编辑操作:
This is the edit action on 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'));
}
这篇关于Laravel/Ardent/用户模型编辑+保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!