Laravel form validation, how to validate and change (transform) the data?

$validator = Validator::make($request->all(), [
            // 
            "id" => "required|integer"
            "title" => "required|unique:posts|max:255",
            "body" => "required",
        ]);
The

requirement is like this. For example, the message from id is 5, which can be verified by Laravel"s integer, but it is actually a string type, so if I don"t strongly convert it to int later, the program will make an error. Although I can convert myself later, I don"t think it"s good to write this way. I want to convert the data at the same time of verification.
so is there any way to validate and transform data at the same time? Thank you.


I have solved it in other ways. After looking for it for a long time and thinking about it for a long time, it seems that I can't make the transformation directly in the verification, but I have come up with a better solution, which is as follows:

Laravel has middleware. We usually do some Filter HTTP request operations in the middleware, but we can also do a lot of "request preprocessing" operations, such as Laravel built-in TrimStrings middleware and ConvertEmptyStringsToNull middleware, both of which will preprocess the requested parameters. For specific use, please see the source code.

so, my solution is to create a ConvertNumericStringsToInt middleware:

class ConvertNumericStringsToInt extends TransformsRequest
{
    /**
     * The attributes that should not be trimmed.
     *
     * @var array
     */
    protected $except = [
        //
    ];

    /**
     * Transform the given value.
     *
     * @param  string $key
     * @param  mixed $value
     * @return mixed
     */
    protected function transform($key, $value)
    {

        $transform = false;
        if ($key === 'id') {

            //  id
            $transform = true;
        } else if (1 === preg_match('/^[a-zA-Z][0-9a-zA-Z]*_id$/', $key)) {

            //  *_id
            $transform = true;
        } else if (1 === preg_match('/^[a-zA-Z][0-9a-zA-Z]*Id$/', $key)) {

            //  *Id
            $transform = true;
        }

        if ($transform) {

            if (!is_numeric($value)) {

                // (  )
            }

            return is_numeric($value) ? intval($value) : $value;
        }

        // 
        return $value;
    }
}

in this way, as long as the parameter we pass is id, or _ id (user_id), or Id (such as userId),) can be detected, once it is found that it is not a number, it will be handled (such as throwing an exception), if it is a number, it will be strongly converted to the int type, and we will not have to do any processing in the program after that.

decide whether to apply this middleware globally according to your own usage.


there is a way. In Laravel 5.6.There is a hook function that ends validation ( after ), which is automatically called when verification is completed:

$validator = Validator::make(...);

$validator->after(function ($validator) {
    if ($this->somethingElseIsInvalid()) {
        $validator->errors()->add('field', 'Something is wrong with this field!');
    }
});

if ($validator->fails()) {
    //
}

official documentation link: https://laravel.com/docs/5.6/.

there is another method (temporarily thought of, not specifically tried). You can add a verification rule of numeric to the verification rule of id :

...
'id' => 'required|integer|numeric',
...
Menu