Tuesday, February 28, 2017

laravel 5.4 modify data before validation in request

I have my custom Request, which extends the Backpack CrudController.

Now I would like to override the prepareForValidation of the ValidatesWhenResolvedTrait since it looks like the right place to modify my incoming data, but I can't figure out how ...

So my first question is, can I override this method? Its protected ...

protected function prepareForValidation()

And my second question, how can I modify my input on the Request or FormRreuqest objects?

Here is my RequestClass

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Config;

class DonationsRequest extends \Backpack\CRUD\app\Http\Requests\CrudRequest
{


    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        // only allow updates if the user is logged in
        return \Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|max:255',
            'email' => 'required|email',
            'dob' => 'required|date',
            'newsletter' => 'required|boolean',
            'country' => 'sometimes|required|in:'.implode(',', Config::get('validation.countries')),
            'street' => 'sometimes|required|string|max:255',
            'zip' => 'sometimes|required|string|between:4,5',
            'city' => 'sometimes|required|string|between:4,255',
            'amount' => 'required|numeric|between:1,'.Config::get('donations.max'),
            'type' => 'required|in:oo,monthly',
            'provider' => 'sometimes|string|nullable',
            'product_id' => 'sometimes|exists:products,id|nullable',
            'campaign_id' => 'required|exists:campaigns,id',
            'status' => 'sometimes|required|in:pending,canceled,success,error',
            'profile' => 'sometimes|string|http://regex:/^profile[0-9]+$/|nullable',
        ];
    }

    /**
     * Get the validation attributes that apply to the request.
     *
     * @return array
     */
    public function attributes()
    {
        return [
            //
        ];
    }

    /**
     * Get the validation messages that apply to the request.
     *
     * @return array
     */
    public function messages()
    {
        return [
            //
        ];
    }

    private function prepareForValidation()
    {

        dd('getValidatorInstance custom');

        $this->sanitizeInput();

        return parent::getValidatorInstance();
    }

    private function sanitizeInput()
    {

        dd('sanitizeInput custom');

        $data = $this->all();

        dd($data);

        // overwrite the newsletter field value to match boolean validation
        $data['newsletter'] = ($data['newsletter'] == 'true' || $data['newsletter'] == '1' || $data['newsletter'] == true) ? true : false;

        return $data;
    }

    private function validate() {
        dd('validate');
    }
}

As you can see, I first tried to override the getValidatorInstance method, since this looked like the common aproach to this, but it is not executed (so not overridden - protected?).




via Thomas Venturini

Advertisement