簡體   English   中英

需要自定義密鑰才能在Laravel 5中注冊

[英]Require custom key to register in Laravel 5

我正在處理的當前應用程序要求用戶只有在獲得注冊密鑰后才能進行注冊。 在Laravel 5中,我可以將其放在Validator中:

return Validator::make($data, [
    // some validation fields...
    'registration_key' => 'required|same:'.$registration_key
]);

這將要求registration_key字段與變量$registration_key相同。 但是,一旦輸入了錯誤的密鑰,視圖中的錯誤消息就會顯示該密鑰:

錯誤:注冊密鑰和[密鑰值]必須匹配。

顯然,我不想向用戶顯示密鑰。 我知道Laravel有一些文檔來展示如何擴展Validator外觀,但是解釋不充分,我無法弄清楚。 有沒有人做過類似的事情並且可以幫助我?

Validator::make($data, [
        // some validation fields...
        'registration_key' => 'required|same:' . $registration_key
    ], [
        'registration_key.same' => 'your text for the error message'
    ]);

我不知道您是否可以在請求類中聲明自己的錯誤消息,但是可以在Controller中檢查它,然后添加錯誤消息。

有關更多信息,請檢查: http : //laravel.com/docs/5.1/validation#custom-error-messages

更新:

感謝Josh Janusch提供了更簡單的解決方案

class MyRequest extends Request
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $registration_key = 'your key';
        return [
            'registration_key' => 'required|same:'.$registration_key
        ];
    }


    /**
     * Set custom messages for validator errors.
     *
     * @return array
     */
    public function messages()
    {
        return ['registration_key.same' => 'your key is not listed'];
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM