Laravel 5.1 Modify input before form request validation

后端 未结 5 954
感情败类
感情败类 2020-12-20 21:05

Is there a way to modify input fields inside a form request class before the validation takes place?

I want to modify some input date fields as follows but it doesn

5条回答
  •  情话喂你
    2020-12-20 21:38

    As of laravel 5.4 you can override the prepareForValidation method of the ValidatesWhenResolvedTrait to modify any input. Something similar should be possible for laravel 5.1.

    Example in your Request

    /**
     * Modify the input values
     *
     * @return void
     */
    protected function prepareForValidation() {
    
        // get the input
        $input = array_map('trim', $this->all());
    
        // check newsletter
        if (!isset($input['newsletter'])) {
            $input['newsletter'] = false;
        }
    
        // replace old input with new input
        $this->replace($input);
    }
    

提交回复
热议问题