2

i got object with some validationl, and the request validated method returns even fields that are not in validation rules. Here is how my validation class looks like.

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'terminal' => ['required'],
        'terminal.followings' => ['boolean']
    ];
}

And this is how my json body on request looks like.

{
    "client" : {
        "pk" : "2128436070",
        "username" : "miljan_rakita",
        "daily_following" : 1000
    },
    "terminal" : {
        "followings" : true,
        "followingasdad" : "asdasdas",
        "followers" : false,
        "exact_account" : true
    }
}

From this validation, i expect to get only this object:

{
    "terminal" : {
        "followings" : true,
    }
}

But when i dd the validated data:

public function store(Test $request)
{
    dd($request->validated());
}

Response is full terminal object, even non validated items.

array:1 [▼
        "terminal" => array:4 [▼
            "followings" => true
            "followingasdad" => "asdasdas"
            "followers" => false
            "exact_account" => true
        ]
    ]

So i expect to get only terminal object with followings field with it. But i get all other fields that are in terminal object.

I can pull out only items i need from array, but i would like to know why i don't get only validated items.

3
  • try to add protected $fillable = ['terminal.followings']; in your model Commented Aug 26, 2019 at 2:43
  • 2
    It has nothing to do with the model, this is with request and validation. @Joseph Commented Aug 26, 2019 at 2:44
  • oh i remember now yo need to pass your rule class in the store methodand it will give you new instance of the validation array Commented Aug 26, 2019 at 2:47

1 Answer 1

1

The form validation only checks if the incoming request does at least contain all required values, so the request data you receive is as expected. Having said so you'd like to adjust the incoming data, which can be done in the latest release; laravel v5.8.33. You can do so by implementing the passedValidation() method in your formrequest (Test in your case).

public function passedValidation()
{
    // '$this' contains all fields, replace those with the ones you want here
}
Sign up to request clarification or add additional context in comments.

Comments

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.