在Laravel 5.1上验证之前修改输入

Pac*_*zco 16 php validation sanitization laravel-5

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

这是我的Request课程 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);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的自定义验证规则 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    
}
Run Code Online (Sandbox Code Playgroud)

当我调用调试时,我获得了这个输出:

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]
Run Code Online (Sandbox Code Playgroud)

似乎sanitize()有效,但是当在验证类上处理值时,它尚未被清理.

sho*_*ild 19

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

重点是,如果更改rules()函数中的Request Values,它对Validator没有影响.

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

protected function getValidatorInstance() {
    $this->sanitize();
    return parent::getValidatorInstance();
}
Run Code Online (Sandbox Code Playgroud)

这会覆盖父级的getValidatorInstance();

父的getValidatorInstance()方法包括

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

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

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

更新(L5.5)

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

    $requestData = $request->all();

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

    $request->replace($requestData);

    $values = $this->validate($request, $rules);
Run Code Online (Sandbox Code Playgroud)


Eni*_*jar 7

您可以通过修改请求并设置输入值来完成此操作。

$request->request->set('key', 'value');
Run Code Online (Sandbox Code Playgroud)

或者,如果您喜欢request辅助方法。

request()->request->set('key', 'value');
Run Code Online (Sandbox Code Playgroud)