Php Laravel 5在请求返回失败时返回模型数据
编辑Php Laravel 5在请求返回失败时返回模型数据,php,validation,laravel,laravel-5,Php,Validation,Laravel,Laravel 5,编辑 use Illuminate\Foundation\Http\FormRequest; abstract class Request extends FormRequest { // } 这个问题已经解决了,但我不明白为什么答案有效。 我想知道为什么它不起作用。 有人能给我解释一下吗 原始问题 use Illuminate\Foundation\Http\FormRequest; abstract class Request extends FormRequest {
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
这个问题已经解决了,但我不明白为什么答案有效。我想知道为什么它不起作用。 有人能给我解释一下吗
原始问题
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
我被我的设置表卡住了,我的问题是,在设置表中,你可以输入一些电子邮件设置,但你也可以更改密码
电子邮件设置和密码重置工作正常,我的表单会自动填充当前用户的数据但是当表单验证失败时,它会将我重定向回没有表单数据的表单
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
我不确定我是否说得很清楚,但下面的代码将对此进行解释
ChangeUserSettingRequest.php
class ChangeUserSettingsRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
if (\Auth::check()) {
return true;
}
return false;
}
/**
* Get the validation rules that apply to the request.
*
* WHEN THIS VALIDATION FAILS IT GOES BACK TO SETTINGS.BLADE.PHP
* BUT IT DOES NOT KEEP THE SETTINGS DATA IN THE FORM
*/
public function rules()
{
return [
'current_password' => 'sometimes|required_with:password',
'password' => 'required_with:current_password|min:8|confirmed',
'password_confirmation' => 'required_with:password',
];
}
}
{!! Form::model($settings, array('url' => route('store.settings'), 'class' => 'col s12')) !!}
<p>@lang('forms.info.settings')</p>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('info_mail', 0, false, ['id' => 'info_mail']) !!}
{!! Form::label('info_mail', Lang::get('forms.newsletter'), ['for' => 'info_mail']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('message_notification', 0, false, ['id' => 'message_notification']) !!}
{!! Form::label('message_notification', Lang::get('forms.messages'), ['for' => 'message_notification']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('friend_notification', 0, false, ['id' => 'friend_notification']) !!}
{!! Form::label('friend_notification', Lang::get('forms.friendrequest'), ['for' => 'friend_notification']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('item_notification', 0, false, ['id' => 'item_notification']) !!}
{!! Form::label('item_notification', Lang::get('forms.reactiononitem'), ['for' => 'item_notification']) !!}
</div>
@if ($settings and $settings->google_maps !== null)
<div class="settings-explain">
<p class="margin-top-20">@lang('forms.info.companysettings')</p>
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('type', 0, false, ['id' => 'type']) !!}
{!! Form::label('type', Lang::get('forms.companytype'), ['for' => 'type']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('google_maps', 0, false, ['id' => 'google_maps']) !!}
{!! Form::label('google_maps', Lang::get('forms.companymap'), ['for' => 'google_maps']) !!}
</div>
@endif
<div class="settings-explain">
<p class="margin-top-20">@lang('forms.info.changepassword')</p>
</div>
<div class="input-field col s12">
{!! Form::label('current_password', $errors->has('current_password') ? $errors->first('current_password') : Lang::get('forms.currentpassword'), ['for' => 'current_password']) !!}
{!! Form::password('current_password', ['class' => $errors->has('current_password') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::label('password', $errors->has('password') ? $errors->first('password') : Lang::get('forms.newpassword'), ['for' => 'password']) !!}
{!! Form::password('password', ['class' => $errors->has('password') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::label('password_confirmation', $errors->has('password_confirmation') ? $errors->first('password_confirmation') : Lang::get('forms.repeatpassword'), ['for' => 'password_confirmation']) !!}
{!! Form::password('password_confirmation', ['class' => $errors->has('password_confirmation') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::button(Lang::get('forms.save'), ['class' => 'btn waves-effect waves-light', 'type' => 'submit', 'name' => 'Save']) !!}
</div>
{!! Form::close() !!}
/**
* Function shows the settings form
*/
public function showSettings()
{
$title = Lang::get('titles.settings');
$user_id = Auth::User()->id;
$settings = $this->settings->getUserSettings($user_id);
$companySettings = $this->companySettings->getSettings($user_id);
if ($companySettings) {
$settings->type = $companySettings->type;
$settings->google_maps = $companySettings->google_maps;
}
return view('pages.users.settings', ['title' => $title])->with(compact('settings'));
}
/**
* Function stores the setting changes
*
* ChangeUserSettingsRequest makes sure that the request is valid
*/
public function storeSettings(ChangeUserSettingsRequest $request)
{
$id = Auth::User()->id;
$success = $this->settings->handleStoreSettingsRequest($request);
// Checks if user has company settings
$hasCompanySettings = $this->companySettings->checkForSettings($id);
// If user has company settings
if ($hasCompanySettings === true) {
// Update company settings
$this->companySettings->updateSettings($request);
}
if ($success === true) {
/* Get translated message */
$message = Lang::get('toast.settingsstored');
return Redirect::route('user.profile', array(Auth::User()->permalink))->withMessage($message);
}
$settings = $this->settings->getUserSettings($id);
/* Get translated message */
$message = Lang::get('forms.error.wrongpassword');
/* This works and the form is filled with the correct data after it redirects me back */
return Redirect::back()->withErrors(array('current_password' => $message))->withSettings($settings);
}
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
settings.blade.php
class ChangeUserSettingsRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
if (\Auth::check()) {
return true;
}
return false;
}
/**
* Get the validation rules that apply to the request.
*
* WHEN THIS VALIDATION FAILS IT GOES BACK TO SETTINGS.BLADE.PHP
* BUT IT DOES NOT KEEP THE SETTINGS DATA IN THE FORM
*/
public function rules()
{
return [
'current_password' => 'sometimes|required_with:password',
'password' => 'required_with:current_password|min:8|confirmed',
'password_confirmation' => 'required_with:password',
];
}
}
{!! Form::model($settings, array('url' => route('store.settings'), 'class' => 'col s12')) !!}
<p>@lang('forms.info.settings')</p>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('info_mail', 0, false, ['id' => 'info_mail']) !!}
{!! Form::label('info_mail', Lang::get('forms.newsletter'), ['for' => 'info_mail']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('message_notification', 0, false, ['id' => 'message_notification']) !!}
{!! Form::label('message_notification', Lang::get('forms.messages'), ['for' => 'message_notification']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('friend_notification', 0, false, ['id' => 'friend_notification']) !!}
{!! Form::label('friend_notification', Lang::get('forms.friendrequest'), ['for' => 'friend_notification']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('item_notification', 0, false, ['id' => 'item_notification']) !!}
{!! Form::label('item_notification', Lang::get('forms.reactiononitem'), ['for' => 'item_notification']) !!}
</div>
@if ($settings and $settings->google_maps !== null)
<div class="settings-explain">
<p class="margin-top-20">@lang('forms.info.companysettings')</p>
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('type', 0, false, ['id' => 'type']) !!}
{!! Form::label('type', Lang::get('forms.companytype'), ['for' => 'type']) !!}
</div>
<div class="input-field col s12 m6 l6">
{!! Form::checkbox('google_maps', 0, false, ['id' => 'google_maps']) !!}
{!! Form::label('google_maps', Lang::get('forms.companymap'), ['for' => 'google_maps']) !!}
</div>
@endif
<div class="settings-explain">
<p class="margin-top-20">@lang('forms.info.changepassword')</p>
</div>
<div class="input-field col s12">
{!! Form::label('current_password', $errors->has('current_password') ? $errors->first('current_password') : Lang::get('forms.currentpassword'), ['for' => 'current_password']) !!}
{!! Form::password('current_password', ['class' => $errors->has('current_password') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::label('password', $errors->has('password') ? $errors->first('password') : Lang::get('forms.newpassword'), ['for' => 'password']) !!}
{!! Form::password('password', ['class' => $errors->has('password') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::label('password_confirmation', $errors->has('password_confirmation') ? $errors->first('password_confirmation') : Lang::get('forms.repeatpassword'), ['for' => 'password_confirmation']) !!}
{!! Form::password('password_confirmation', ['class' => $errors->has('password_confirmation') ? 'invalid' : '']) !!}
</div>
<div class="input-field col s12">
{!! Form::button(Lang::get('forms.save'), ['class' => 'btn waves-effect waves-light', 'type' => 'submit', 'name' => 'Save']) !!}
</div>
{!! Form::close() !!}
/**
* Function shows the settings form
*/
public function showSettings()
{
$title = Lang::get('titles.settings');
$user_id = Auth::User()->id;
$settings = $this->settings->getUserSettings($user_id);
$companySettings = $this->companySettings->getSettings($user_id);
if ($companySettings) {
$settings->type = $companySettings->type;
$settings->google_maps = $companySettings->google_maps;
}
return view('pages.users.settings', ['title' => $title])->with(compact('settings'));
}
/**
* Function stores the setting changes
*
* ChangeUserSettingsRequest makes sure that the request is valid
*/
public function storeSettings(ChangeUserSettingsRequest $request)
{
$id = Auth::User()->id;
$success = $this->settings->handleStoreSettingsRequest($request);
// Checks if user has company settings
$hasCompanySettings = $this->companySettings->checkForSettings($id);
// If user has company settings
if ($hasCompanySettings === true) {
// Update company settings
$this->companySettings->updateSettings($request);
}
if ($success === true) {
/* Get translated message */
$message = Lang::get('toast.settingsstored');
return Redirect::route('user.profile', array(Auth::User()->permalink))->withMessage($message);
}
$settings = $this->settings->getUserSettings($id);
/* Get translated message */
$message = Lang::get('forms.error.wrongpassword');
/* This works and the form is filled with the correct data after it redirects me back */
return Redirect::back()->withErrors(array('current_password' => $message))->withSettings($settings);
}
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
Request.php
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
因此,我的问题是,在我的UserInfoController中,我重定向回表单,它具有良好的数据,但当我的ChangeUserSettingsRequest将我重定向回表单时,表单是空的
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
有人知道为什么ChangeUserSettingsRequest不返回数据吗
- 我说的不是密码数据,而是电子邮件设置李>
- 我不想做一个肋骨验证器,因为我认为这应该是可行的
请注意:当验证失败时,功能
storeSettings
将完全不执行失败时,将其返回页面,并返回其输入数据(withInput())
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
有关更多信息,请参阅与请求相关的Laravel文档 我找到了
formRequest本应正确处理它,但不知道为什么没有正确处理。我在查看formRequest.php文件时找到了解决方案 我必须覆盖响应功能,它现在可以工作了
use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {
//
}
我唯一需要添加到我的ChangeUserSettingsRequest
中的是
public function response(array $errors)
{
return $this->redirector->to($this->getRedirectUrl())->withErrors($errors, $this->errorBag);
}
唯一的区别是我没有将输入字段发回,这有点奇怪,但我很高兴它现在已经解决了。尝试用settings($settings)替换'->;'使用“->withInputs();”在里面controller@pinkalvansia控制器中的部件工作正常。只是先执行
ChangeUserSettingRequest
。如果失败,请求将永远不会到达控制器。因此,当ChangeUserSettingRequest
失败时,它应该会将我和表单数据一起发送回表单。您是否可以使用showSettings()中的dump($request->input())
输出更新您的问题
更改用户设置请求失败后的方法?@Björn在Laravel 5中,当更改用户设置请求失败时,它永远不会到达控制器。它会自动将您送回表单。是的,我知道,showSettings
方法是表单正确呈现的地方吗?如果您dump($request->input())
在那里,您可以看到laravel从ValidationRequest发回的所有输入数据。正如您在文件底部所看到的UserInfoController
我已经在做类似的事情了,不完全是这样,但工作原理相同。我的问题是,当ChangeUserSettingsRequest
失败时,它会直接将我重定向回控制器,因此如果它到达控制器,它就永远不会到达控制器。我不会有这个问题。我已经奖励了你赏金,因为你已经给了我一些答案。它并没有直接解决我的问题,但我认为这个答案将来会帮助其他人。你能不能扩展FormRequest而不是Request?这也能解决问题吗?@JonasHartmann我更新了这个问题。但这并没有帮助导致类请求扩展formRequest。