久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Laravel 5 Form Request 數據預操作

Laravel 5 Form Request data pre-manipulation(Laravel 5 Form Request 數據預操作)
本文介紹了Laravel 5 Form Request 數據預操作的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在處理一個表單,用戶可以在其中更新他們的出生日期.該表單為用戶提供了 3 個單獨的字段,分別為 daymonthyear.在服務器端,我當然想將這 3 個單獨的字段視為一個值,即 yyyy-mm-dd.

I'm processing a form where a user can update their date of birth. The form gives the user 3 separate fields for day, month and year. On the server-side of course I want to treat these 3 separate fields as one value i.e. yyyy-mm-dd.

所以在驗證和更新我的數據庫之前,我想通過連接 yearmonthday 使用 - 字符創建我需要的日期格式(并且可能取消設置原始 3 個字段).

So before validation and updating my database, I want to alter the form request to create a date_of_birth field by concatenating year, month and day with - characters to create the date format I need (And possibly unset the original 3 fields).

用我的控制器手動實現這一點不是問題.我可以簡單地獲取輸入,將由 - 字符分隔的字段連接在一起并取消設置它們.然后,我可以在傳遞給處理處理的命令之前手動驗證.

Achieving this manually with my controller is not a problem. I can simply grab the input, join the fields together separated by - characters and unset them. I can then validate manually before passing off to a command to deal with the processing.

但是,我更喜歡使用 FormRequest 來處理驗證并將其注入到我的控制器方法中.因此,我需要一種在執行驗證之前實際修改表單請求的方法.

However, I would prefer to use a FormRequest to deal with the validation and have that injected into my controller method. Therefore I need a way of actually modifying the form request before validation is executed.

我確實發現了以下類似的問題:Laravel 5 請求 - 更改數據

I did find the following question which is similar: Laravel 5 Request - altering data

它建議覆蓋表單請求上的 all 方法,以包含在驗證之前操作數據的邏輯.

It suggests overriding the all method on the form request to contain the logic for manipulating the data prior to validation.

<?php namespace AppHttpRequests;

class UpdateSettingsRequest extends Request {

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [];
    }

    public function all()
    {
        $data = parent::all();
        $data['date_of_birth'] = 'test';
        return $data;
    }

這對驗證來說一切都很好,但是覆蓋 all 方法實際上并沒有修改表單請求對象上的數據.所以在執行命令時,表單請求包含原始未修改的數據.除非我使用現在覆蓋的 all 方法來提取數據.

This is all well and good for the validation, but overriding the all method doesn't actually modify the data on the form request object. So when it comes to executing the command, the form request contains the original unmodified data. Unless I use the now overridden all method to pull the data out.

我正在尋找一種更具體的方法來修改表單請求中的數據,而無需調用特定方法.

I'm looking for a more concrete way to modify the data within my form request that doesn't require the calling of a specific method.

干杯

推薦答案

在 laravel 5.1 中你可以做到

in laravel 5.1 you can do that

<?php namespace AppHttpRequests;

class UpdateSettingsRequest extends Request {

public function authorize()
{
    return true;
}

public function rules()
{
    return [];
}

protected function getValidatorInstance()
{
    $data = $this->all();
    $data['date_of_birth'] = 'test';
    $this->getInputSource()->replace($data);

    /*modify data before send to validator*/

    return parent::getValidatorInstance();
}

這篇關于Laravel 5 Form Request 數據預操作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

add new element in laravel collection object(在 Laravel 集合對象中添加新元素)
Creating an edit modal in Laravel 5(在 Laravel 5 中創建編輯模式)
Laravel 5.5 API resources for collections (standalone data)(用于集合的 Laravel 5.5 API 資源(獨立數據))
What is the best practice to create a custom helper function in php Laravel 5?(在 php Laravel 5 中創建自定義輔助函數的最佳實踐是什么?)
No #39;Access-Control-Allow-Origin#39; header - Laravel(沒有“Access-Control-Allow-Origin標頭 - Laravel)
Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登錄頁面)
主站蜘蛛池模板: 亚洲狠狠爱一区二区三区 | 久草新在线 | 成人久久久 | 精品一区在线免费观看 | 色永久| 久久福利网站 | 国产精品日韩欧美一区二区三区 | 免费在线观看av片 | 在线观看精品视频网站 | 亚洲精品久久久久久国产精华液 | 中文字幕一区在线观看视频 | 97人人澡人人爽91综合色 | 亚洲高清视频在线 | 欧美精品一二三 | 欧美日韩黄色一级片 | 国产乱精品一区二区三区 | 国产精品a久久久久 | 韩日在线视频 | 色久影院 | 欧美一区免费在线观看 | 91色站 | 综合久久99 | 日本三级全黄三级三级三级口周 | 免费在线观看黄色av | 亚洲一区二区三区 | 欧美在线观看一区 | 毛片入口 | 四虎影院在线观看免费视频 | 五月综合激情婷婷 | 亚洲毛片在线观看 | 精品久久国产视频 | 久久精品一级 | 亚洲国产精品久久久 | 亚洲精品久久久一区二区三区 | 男女精品网站 | 国产精品久久 | 欧洲一区二区在线 | 国产精品美女www | 日韩免费一区 | 亚洲欧美视频一区 | 成人av在线播放 |