dongwen3093 2015-06-11 19:00
浏览 92
已采纳

失败请求的Laravel 5返回模型数据

EDIT
This issue has been solved, but I don't understand why the answer worked.
I would like to know why it didnt work. Is there some one who could explain me?


Original question
I am stuck on my settings form, my problem is that at the settings form you can enter some email settings but you can also change your password.

The email settings and password reset works and my form fills it self with the data of the current user. But when the form validation fails it redirecteds me back to the form without the form data.

I am not sure if I made myself clear, but this code below will explain it.

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',
        ];
    }

}

settings.blade.php

{!! 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() !!}

UserInfoController.php

/**
 *  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);
}

Request.php

use Illuminate\Foundation\Http\FormRequest;
abstract class Request extends FormRequest {

    //

}

So my problem is that in my UserInfoController I redirect back and it has the good data but when my ChangeUserSettingsRequest redirects me back the form is empty.

Does anyone know why ChangeUserSettingsRequest does not send the data back?

  • I am not talking about the password data but about the email settings.
  • I do not want to make a costum validator as I think this should be posible.

Please note: when the validation fails, the function storeSettings will not be executed at all

  • 写回答

2条回答 默认 最新

  • douzhaocheng4533 2015-06-16 21:53
    关注

    I found it!

    The formRequest should have handled it correctly but no idea why it did not.
    I found the solution when looking trough the file formRequest.php https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Http/FormRequest.php

    I had to override the response function and it works now!
    To only thing that I had to add to my ChangeUserSettingsRequest was this

    public function response(array $errors)
    {
        return $this->redirector->to($this->getRedirectUrl())->withErrors($errors, $this->errorBag);
    }
    

    The only difference is that I don't send the input fields back, little strange that this works but I am glad that it is solved now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题