2

I get from response a formData, that I change into a middleware to use it on validation, the problem, is that I must do another modification to validate all elements from array without using 'array.oneElement'.

Code from Middleware

public function handle($request, Closure $next)
{
    if ($request->has('all_values')) {
        $request->merge([
            'all_values' => json_decode($request->get('all_values'), true)
        ]);
    }

    return $next($request);
}

// From Requst

public function rules()
{
    $validations = [
        'all_values.saleforce_id'                    => 'required',
        'all_values.customer_id'                     => 'required',
        'all_values.lkp_product_category_id'         => 'required',
    ]
    // I need to access this all_values before validations

    if (request()->has('validation_field')) {
            return [request()->validation_field => $validations[ request()->validation_field ]];
        } // this is used for a validation , to do validation onChange

    return $validations;
}


  $request = $request->all()['all_values']; // I need something like this, but this doesn't work
1
  • No need for controller, for request the Request Resource is accessed first then the controller, and I need to modify the request before that, in middleware or in Request Resource Commented Mar 5, 2020 at 9:47

1 Answer 1

2

On FormRequests you can use prepareForValidation().

protected function prepareForValidation()
{
    $this->merge([
        'all_values' => json_decode($request->get('all_values'), true),
    ]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Actually works for my validation method, but on submit I get error validation, like I have no field.
whats the difference between validation method and submit?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.