0

I would like to use FormRequest validation in which allow request fields only in rules() return array keys.

In the below code, I would like to allow request fields only 'os', 'number', 'version'. If request include the other field , return error response.

How can I modify the code ?

public function rules()
  {
    return [
        'os' => [
            'required',
            \Rule::in(['android', 'ios']),
        ],
        'number' => 'required|integer',
        'version' => ['required', 'regex:/^\d+.\d+.\d+$/'],
    ];
  }

1 Answer 1

1

There is a way you can do this using form request. It may not send the proper response but it works. In your Form Request's authorize method use the following code.

public function authorize ()
{
    $params = $this->request->keys();
    $os_status = in_array('os', $params);
    $number_status = in_array('number', $params);
    $version_status = in_array('version', $params);

    $check = $os_status & $number_status & $version_status & (count($params) != 3 ? false : true);

    return $check;
}

It will return HTTP response with 403 status code.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.