在Laravel 5.1上验证之前修改输入 [英] Modify input before validation on Laravel 5.1

查看:81
本文介绍了在Laravel 5.1上验证之前修改输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在验证成功之前修改用户提交的输入.我遵循了这很简单说明,但是当我在Laravel 5.1上对其进行测试时,它无法正常工作. 我做错什么了吗?

I'm trying to modify an user submitted input before validation success. I've followed this easy instructions, but when I test it on Laravel 5.1, It's not working. Am I doing something wrong?

这是我在SSHAM\Http\Requests\UserCreateRequest.php

<?php

namespace SSHAM\Http\Requests;

use SSHAM\Http\Requests\Request;

class UserCreateRequest extends Request
{

    // Some stuff not related with this problem

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        // Only for debug
        $prova = $this->all();
        echo "<pre>Inside Request - Before sanitize\n[" . $prova['public_key'] . "]</pre>\n";

        // Call a function to sanitize user input
        $this->sanitize();

        // Only for debug    
        $prova = $this->all();
        echo "<pre>Inside Request - After sanitize\n[" . $prova['public_key'] . "]</pre>\n";

        return [
            'username' => 'required|max:255|unique:users',
            'public_key' => 'openssh_key:public',
        ];
    }

    /**
     * Sanitizes user input. In special 'public_key' to remove carriage returns
     */
    public function sanitize()
    {
        $input = $this->all();

        // Removes carriage returns from 'public_key' input
        $input['public_key'] = str_replace(["\n", "\t", "\r"], '', $input['public_key']);

        $this->replace($input);
    }

}

这是我在SSHAM\Providers\OpenSSHKeyValidatorServiceProvider.php

<?php

namespace SSHAM\Providers;

use Illuminate\Support\ServiceProvider;

class OpenSSHKeyValidatorServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        // Registering the validator extension with the validator factory
        \Validator::extend('openssh_key', function ($attribute, $value, $parameters) {

            // Some stuff not related with this problem    

            // Only for debug
            echo "<pre>Inside Validator value\n[" . $value ."]</pre>\n";
            dd();

            return true;
        });

    }

    // Some stuff not related with this problem    
}

当我致电进行调试时,我得到以下输出:

When I call for debugging I obtain this output:

Inside Request - Before sanitize
[blah 
second line 
third line]

Inside Request - After sanitize
[blah second line third line]

Inside Validator value
[blah 
second line 
third line]

似乎sanitize()在工作,但是在验证类上处理值时,尚未对其进行消毒.

Seems that sanitize() is working, but when value is treated on validation class it has not been sanitized.

推荐答案

这是一个棘手的问题.我只是想出一种方法来实现您想要的.

This is a tricky one. I only figured out one way to achieve what you want.

主要要点是,如果您在 rules()函数中更改请求值,则对验证程序没有影响.

The main point is, that it has no effect for the Validator if you change the Request Values in the rules() function.

您可以通过向UserCreateRequest中添加函数来解决此问题:

You could do a workaround by adding a function to your UserCreateRequest:

protected function getValidatorInstance() {
    $this->sanitize();
    return parent::getValidatorInstance();
}

这将覆盖父级的getValidatorInstance();

This overrides the parent's getValidatorInstance();

父级的getValidatorInstance()方法包括

The parent's getValidatorInstance() method includes

    return $factory->make(
        $this->all(), $this->container->call([$this, 'rules']), $this->messages(), $this->attributes());

在rules()函数中的代码之前到达哪个位置,因此使用$ this-> all()的旧值(不受rules()中的更改影响).

Which is reached before your code in the rules() function, so the old values (not affected by the changes in rules()) of $this->all() are used.

如果您在自己的RequestClass中覆盖该函数,则可以在调用实际父级的方法之前操纵Request值.

If you override that function in your own RequestClass you can manipulate the Request values before calling the actual parent's method.

更新(L5.5)

如果您使用的是控制器验证功能,则可以执行以下操作:

If you are using the Controllers validate function you could do something like that:

    $requestData = $request->all();

    // modify somehow
    $requestData['firstname'] = trim($requestData['firstname']);

    $request->replace($requestData);

    $values = $this->validate($request, $rules);

这篇关于在Laravel 5.1上验证之前修改输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆