2

Front End Part

The parameters are being sent like this:

Front End sending data

Laravel Request

class CarCreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        //TODO: Define authorization logic, possibly a middleware
        return true;
    }  

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'car.name' => 'present|required'
        ];
    }
}

Real Problem

The request class always validates to false. I checked the Validating Array section, but it looks like this works sending parameters like this:

car[name]=Spidey Mobile

However, I need to send this data stringified using JSON.stringify().

Is there a workaround for this? It looks like dot notation isn't working since this is a JSON string rather than an array. I've tried modifying the request data before being evaluated, but I haven't found anything that works for Laravel 5.7.

4
  • how did you modify the request data before being evaluated? That would be your best bet. json_decode your request and set as the validation data. Commented Dec 4, 2018 at 3:17
  • car: {"name": "Spidey Mobile"} does not seem like a valid json format. {"car": {"name": "Spidey Mobile"}} is. Commented Dec 4, 2018 at 10:20
  • Just as a side note, you don't need present and required in your validation as required alone will check that the field is present, as well as it not being empty. Commented Dec 4, 2018 at 13:47
  • @CUGreen I used json_decode within the ValidationData method but it looks like it's no longer available for 5.7. It's not changing anything at all Commented Dec 4, 2018 at 21:06

2 Answers 2

8

Here's the solution, I used both the sanitize and validator method within the request in order to change the request data before being evaluated.

class CarCreateRequest extends FormRequest
{
    /**
    * Determine if the user is authorized to make this request.
    *
    * @return bool
    */
    public function authorize()
    {
        //TODO: Define authorization logic, possibly a middleware
        return true;
    }  

    public function validator($factory)
    {
    return $factory->make(
        $this->sanitize(), $this->container->call([$this, 'rules']), $this->messages()
    );
    }

    public function sanitize()
    {
        $this->merge([
            'car' => json_decode($this->input('car'), true)
        ]);
        return $this->all();
    }

    /**
    * Get the validation rules that apply to the request.
    *
    * @return array
    */
    public function rules()
    {
        return [
            'car.name' => 'present|required'
        ];
    }
}

The json_decode will transform the JSON string into an array that can be validated by Laravel.

Sign up to request clarification or add additional context in comments.

Comments

5

You should just be able to override the validationData method in your Request like this:

protected function validationData()
{
    $this->merge(['car', json_decode($this->car)]); // or what ever your request value is.
    return $this->all();
}

1 Comment

I'm sure that this method is there for a reason? Are you sure you are Casting your request to CarCreateRequest in your controllers store method? Try and dd($this->all()); in validationData() to make sure it is passing through and returning the correct data.

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.