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

shanekerwin's avatar

Ignore form request required rules on update

I'm really surprised to not find this mentioned anywhere, which makes me think I'm doing this wrong. He's my scenario:

I have a form with Name and URL. The Name is a required field. But what if you want to update just the URL? In that case the name is not required, because it's already there. But I want to keep the same formRequest for validation because it contains other validation rules (valid URL, max, min, unique etc.). I have solved the problem like this:

    public function rules()
    {
        $requiredFields = [
            'name'
        ];

        $rules = [
            'name' => 'max:255|unique:tableName,name',
            'url' => 'url|max:255'
        ];

        if ($this->method() == 'POST') {
            foreach ($rules as $field => $rule)
            {
                if (in_array($field, $requiredFields))
                {
                    $rules[$field] .= '|required';
                }
            }
        }

        return $rules;
    }

But ...really? This is not an issue for anyone else? What am I missing?

0 likes
8 replies
shanekerwin's avatar

So this is saying - if the field is there, it cannot be empty? But if it's not there, allow it to be empty?

That's not really what I want either. I want to say this is a required field when creating a new record, but it's not required when updating a record. I want something like:

    'name' => 'requiredOnCreate'
d3xt3r's avatar

Then, have separate rules for create and update depending on your form ... You need to take some initiative, everything can't be served on a silver platter ...

shanekerwin's avatar

I'm wondering if anyone else has run into the same issue, it seems like it would be very common. To only want to ignore specifically the 'required' rule for update requests.

1 like
d3xt3r's avatar

Just a question, are your create form and update form, exactly the same ?

Snapey's avatar

This is what I did in (I think) 5.1

Extend the app/Http/Request.php

/**
     * If the method of the request is a POST then return the rules with additional rule
     * EG for the password field which should only be tested when its a new user
     * @param  array $rules     Array of rules determined so far
     * @param  string $attribute form element that will have rules applied
     * @param  string $rule      the actual rule
     * @return array            the array with the rule added, else the original
     */
    public function postMergeRule($rules,$attribute,$rule)
    {
        if($this->method == 'POST')
        {
            return array_add($rules,$attribute,$rule);
        } 
        return $rules;

The in the form request

        $rules = [
            'name' => 'required|min:5',
            'email' => 'required|email',
        ];

        $this->postMergeRule($rules,'password','required|min:5');

        return $rules;

So in this case, the password field is only required on a Create and not on an Update

The rules() function only has to return an array of fieldnames and rules so there are many ways it could be manipulated.

1 like
Deleter's avatar

Thank you for the idea! I played around with what you've suggested and after a while a had a working solution for me!

artcore's avatar

There's an ignore for that from the docs

Forcing A Unique Rule To Ignore A Given ID:

Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. You will probably want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.

To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit the rules:

use Illuminate\Validation\Rule;

Validator::make($data, [
    'email' => [
        'required',
        Rule::unique('users')->ignore($user->id),
    ],
]);

Please or to participate in this conversation.