Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Garet's avatar
Level 3

Best way of sharing form request for store and update

In my project I tend to re-use a form request for both creating and updating a resource. Sometimes there might be a slight difference, for example when storing a user you want to check that the email address is unique, but when updating a user you want to check that the email address is unique but ignoring their own user id.

I tend to do this like so:

class StoreUser extends FormRequest
{
    public function rules()
    {

        $current_id = $this->getMethod() == 'PATCH'
            ? ',' . $this->route('user')->id
            : '';

        return [
            'name' => 'required|max:80',
            'nickname' => 'max:35',
            'email' => 'required|email|max:80|unique:users,email' . $current_id,
        ];
    }
}

I don't know if this is a good way of doing it or not. It's kind of simple and easy to read.

Another way I thought of is to have 2 classes, one called StoreUser:

class StoreUser extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'required|max:80',
            'nickname' => 'max:35',
            'email' => 'required|email|max:80|unique:users,email',
        ];
    }
}

And the other called UpdateUser which extends StoreUser:

class UpdateUser extends StoreUser
{
    public function rules()
    {
        $rules = parent::rules();
        $rules['email'] .= ',' . $this->route('user')->id;
        return $rules;
    }
}

I'm not sure if the second way is an overkill, or if it's better because it's helping to seperate the two actions.

What do you think?

As an aside, when I have a form request that is identical for both storing and updating a resource, I have been prefixing the class name with the word "Store". For example, StoreDocument, StoreOption, etc. Since these requests aren't specific to storing a resource and are also used for updating, what would better terminology be? DocumentRequest and OptionRequest perhaps?

0 likes
0 replies

Please or to participate in this conversation.