Skip to content

FormRequest is always hit with 'GET' request for 'POST', 'PUT' and 'PATCH' #260

@TimOgilvy

Description

@TimOgilvy

My form request controller is a bit non-standard - it changes the optional-ness of the fields for a patch request, in an attempt to avoid having to repeat the rules for each type. Fair enough right - 'DRY' code?

The trouble is, then I get nothing in my docs. GET returns no rules (why should it?) so the doc contains no rules.

I can hack this by returning the rules on GET irrelevantly, but it doesn't solve the difference between PUT, POST, and PATCH where every field is optional.

Here's my FormRequest rules for example:

$requiredRules = [
    'state_id'          => 'required|integer',
    'name'              => 'required|min:3|max:191',
    'status'             => 'required|string', // ['required', new ProjectStatus],
    'phase'             => 'required|string', // ['required', new ProjectPhase],
    'owner_id'        => 'required|integer',
    'value'              => 'nullable',
    'contact_id'     => 'nullable',
    'customer_id'  => 'nullable',
];
switch($this->method())
{
    case 'PUT':
    case 'POST':
        return $requiredRules;
    case 'PATCH':
        return $this->getPatchRules($requiredRules);
    case 'GET':
    case 'DELETE':
        return [];
}

I'm going to see if I can figure out how to fix this, but any help would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions